C# LINQ TO DataSet:数据表上的多个分组依据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1225710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ TO DataSet: Multiple group by on a data table
提问by Anoop
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
我正在使用 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()
}
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??
现在我想在两列“Coulmn1”和“Column2”上执行分组。谁能告诉我语法或给我一个解释数据表上多个分组依据的链接?
Thanks
谢谢
采纳答案by CMS
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()
}