LINQ: Query CSV files

Programming LINQ C#

Let's assume we have this CSV file "Data.csv":

37,200,25,360,99
153,57,290,24,180
64,32,16,8,4
97,1,999,123,456

Let's assume that the data above shows Quarter wise sales by different SalesPerson. There are 5 columns. The first column is the SalesPersonID and the rest represents total items sold by the salesperson in each quarter.  We will use LINQ to parse the CSV file like this:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ConsoleAppQueryCsvLinq
{
class Program
{
static void Main(string[] args)
{
IEnumerable<string> strCSV = File.ReadLines(@"../../Data.csv");

// query CSV file through LINQ
var results = from str in strCSV
let tmp = str.Split(',')
.Skip(1)
.Select(x => Convert.ToInt32(x))
select new
{
Max = tmp.Max(),
Min = tmp.Min(),
Total = tmp.Sum(),
Avg = tmp.Average()
};

// caching for performance
var query = results.ToList();

foreach (var x in query)
{
Console.WriteLine(
string.Format("Maximum: {0}, " +
"Minimum: {1}, " +
"Total: {2}, " +
"Average: {3}",
x.Max, x.Min, x.Total, x.Avg));
}
Console.ReadLine();
}
}
}

Notice that we do not include the SalesPersonID in our calculations so we use .Skip(1) to not include the first column.

The output result will be:

Maximum: 360, Minimum: 25, Total: 684, Average: 171
Maximum: 290, Minimum: 24, Total: 551, Average: 137,75
Maximum: 32, Minimum: 4, Total: 60, Average: 15
Maximum: 999, Minimum: 1, Total: 1579, Average: 394,75