【问题标题】:LINQ TO DataSet: Multiple group by on a data tableLINQ TO DataSet:数据表上的多个分组依据
【发布时间】:2010-11-16 13:52:09
【问题描述】:

我正在使用 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”两列执行分组。谁能告诉我语法或提供一个链接来解释数据表上的多个分组依据吗??

谢谢

【问题讨论】:

    标签: c# linq datatable dataset group-by


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2010-10-16
      • 2013-08-05
      • 1970-01-01
      相关资源
      最近更新 更多