Tushit Nagda Tushit Nagda - 1 year ago 102
jQuery Question

Write a query for DataTable using LINQ

Current DataTable

Need to Count how many data logged on each day of the week

Desired Output

Whats wrong with this:

var res = from c in dtHour.AsEnumerable()
group c by c.Field<string>("Day Logged") into day
let count = day.Count()
select new {
day_s = day.Count()

The code is not giving the desired output.

Answer Source
var collection = dtHour.AsEnumerable()
      .GroupBy(item => item.Field<string>("Day Logged"))
      .Select(item => new { DayOfWeek = item.Key, Count = item.Count());

To print it the way you wanted:

Console.WriteLine("Day of week - Count");
foreach(var item in collection)
    Console.WriteLine(item.DayOfWeek + " - " + item.Count);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download