0

I have a CSV file which simply contains only numbers per line

example

12,34,56 1,2,3 34,45,67 

I would like to get the maximum ,minimum and average of each row.

I started coding

from str in File.ReadLines(@"FilePath") .Select(x => Convert.ToInt32(x)) 

But I am not sure how to separte the values from CSV file and project the maximum,minimum,average of each row.

If you still need more information , i am happy to provide.

1 Answer 1

1

You could use the following code snippet

 var result = from str in File.ReadLines(@"FilePath") let GetValue = str.Split(',') .Select(x => Convert.ToInt32(x)) select new { Maximum = GetValue.Max(), Minimum = GetValue.Min(), Average = GetValue.Average() }; 

I have simulated the result

 IEnumerable<string> lines = new[] { "1,2,3", "4,5,6", "45,56,67" }; var result = from str in lines let GetValue = str.Split(',') .Select(x => Convert.ToInt32(x)) select new { Maximum = GetValue.Max(), Minimum = GetValue.Min(), Average = GetValue.Average() }; 
Sign up to request clarification or add additional context in comments.

4 Comments

Note you're parsing each int three times here. If perf is a concern, this could be re-written to use Aggregate to only parse once.
Be careful though assuming too much about the format of a CSV file. The format of a number (int or decimal) might be different and contain comma's or dots and there might be lines that are empty or contain text. CSV has more than one standard... en.wikipedia.org/wiki/Comma-separated_values#Lack_of_a_standard
@ Erno de Weerd - :) Let him at least do some customization based on his requirement .The user did not give more information about the CSV file format he is processing.
@Cory Nelson :Well said it could be rewritten to use Aggregate