C# 按列1分组,按列2分组,然后对EnumerablerRowCollection上的列2进行计数

C# 按列1分组,按列2分组,然后对EnumerablerRowCollection上的列2进行计数,c#,linq,count,group-by,C#,Linq,Count,Group By,我在C#.Net中有一个EnumerablerRowCollection,有两列。我想按第一列进行分组,然后是第二列,然后计算第二列 我的EnumerablerRowCollection基于DataGridView上类似以下的DataTable: 如果这是一个MySQL表,我会像这样实现我想要的: SELECT `Product Name`, `Upsell Price`, Count(`Upsell Price`) FROM `tblWhatever`

我在C#.Net中有一个EnumerablerRowCollection,有两列。我想按第一列进行分组,然后是第二列,然后计算第二列

我的EnumerablerRowCollection基于DataGridView上类似以下的DataTable:

如果这是一个MySQL表,我会像这样实现我想要的:

SELECT `Product Name`, 
       `Upsell Price`, 
       Count(`Upsell Price`) 
  FROM `tblWhatever` 
  GROUP BY `Product Name`, `Upsell Price`;
我需要这个LINQ的等价物

我以编程方式构建了DataTable,如下所示:

DataTable myDataTable = new DataTabl();
DataColumn myColumn = new DataColumn("My Column");
myDataTable.Columns.Add(myDataTable);
myDataTable.Rows.Add(new object[] { "whatever" });
myDataGridView.DataSource = myDataTable;
我找到了丹尼尔·谢弗(Daniel Schaffer)的答案,这部分帮助了我完成了整个小组的工作,但我仍停留在计数上,如下所示:

var queryableData = myDataTable.AsEnumerable();
var result = from row in queryableData
          group row by new { ProductName = row["Product Name"], 
                             ProductPrice = row["Product Price"] };
如何编辑LINQ查询以允许我按第一列分组,然后按第二列分组,然后计算第二列?

尝试以下操作:

var result = from row in queryableData
          group row by new { ProductName = row["Product Name"], 
                             ProductPrice = row["Product Price"] } into grp
          select new { grp.Key.ProductName , 
                       grp.Key.ProductPrice, 
                       Count = grp.Count()  };

谢谢你,这正是我想要的!