Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ TO DataSet: Multiple group by on a data table

I am using Linq to dataset to query a datatable. If i want to perform a group by on "Column1" on data table, I use following query

var groupQuery = from table in MyTable.AsEnumerable()
group table by table["Column1"] into groupedTable

select new
{
   x = groupedTable.Key,
   y = groupedTable.Count()
}

Now I want to perform group by on two columns "Coulmn1" and "Column2". Can anybody tell me the syntax or provide me a link explaining multiple group by on a data table??

Thanks

like image 619
Anoop Avatar asked Aug 04 '09 04:08

Anoop


1 Answers

You should create an anonymous type to do a group by multiple columns:

var groupQuery = from table in MyTable.AsEnumerable()
group table by new { column1 = table["Column1"],  column2 = table["Column2"] }
      into groupedTable
select new
{
   x = groupedTable.Key,  // Each Key contains column1 and column2
   y = groupedTable.Count()
}
like image 121
Christian C. Salvadó Avatar answered Oct 17 '22 05:10

Christian C. Salvadó