Use LINQ to group data from DataTable

Viewed 83073

I want to use LINQ to group data from a DataTable (columns: userid, chargetag, charge).

The content could look like this:

userid    chargetag    charge
-----------------------------
user1     tag3         100
user2     tag3         100
user3     tag5         250

I need something like this as a result:

chargetag    count    sum
-------------------------
tag3         2        200
tag5         1        250

This is what I have so far:

var groupedData = from b in dataTable.AsEnumerable()
                  group b by b.Field<string>("chargetag") into g
                  let count = g.Count()
                  select new
                  {
                      ChargeTag = g.Key,
                      Count = count,
                  };

I can extract the name of the chargetag and the number of it. How would I have to change the LINQ query to access the sum of charges as well?

Thanks in advance :-)

Regards, Kevin

1 Answers
Related