LINQ为DataSet:通过在数据表中的多个组 [英] LINQ TO DataSet: Multiple group by on a data table

查看:197
本文介绍了LINQ为DataSet:通过在数据表中的多个组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LINQ到数据集查询的DataTable。如果我想通过在列1的数据表来执行一组,我用下面的查询

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

现在我想通过在两列执行组Coulmn1和列2。谁能告诉我的语法或通过在数据表??

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??

感谢

推荐答案

您应该创建一个匿名类型由多个列做1组:

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

这篇关于LINQ为DataSet:通过在数据表中的多个组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆