LINQ TO DataSet:数据表上的多个分组

Ano*_*oop 12 c# linq datatable group-by dataset

我使用Linq数据集来查询数据表.如果我想在数据表上的"Column1"上执行一个组,我使用以下查询

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

select new
{
   x = groupedTable.Key,
   y = groupedTable.Count()
}
Run Code Online (Sandbox Code Playgroud)

现在我想在两列"Coulmn1"和"Column2"上执行group by.任何人都可以告诉我语法或者在数据表中提供一个解释多个组的链接吗?

谢谢

CMS*_*CMS 16

您应该创建一个匿名类型来按多列执行组:

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()
}
Run Code Online (Sandbox Code Playgroud)