LINQ TO DataSet:在数据表上进行多重分组

12

我正在使用 Linq to dataset 查询数据表。如果我想在数据表上执行按 "Column1" 分组的操作,我可以使用以下查询:

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

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

现在我想对“Coulmn1”和“Column2”这两列进行分组。请问有人能告诉我在数据表上执行多重分组的语法或提供一个链接来解释吗?

谢谢。

1个回答

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()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接