这是我的数据表
Type Product Action Quantity
---------- ------- -------- --------
Fruit Apple Sales 1
Fruit Apple Sales 2
Fruit Orange Sales 3
Fruit Orange Picking 3
Vegetables Carrot Sales 1
Vegetables Carrot Sales 3
Vegetables Carrot Sales 3
Vegetables Carrot Pickings 3
目前我有这个
var tableColumnGrouping =
from table in ReportConfigurationTable.AsEnumerable()
group table by new
{
column1 = table["Type"],
column2 = table["Product"]
}
into groupedTable
select new
{
tableColumn = groupedTable.Key, // Each Key contains column1 and column2
tableColumnCount = groupedTable.Count()
};
这将给我一些代表:
{ column1 = "Fruit", column2 = "Apple" }
{ column1 = "Fruit", column2 = "Orange" }
{ column1 = "Vegetable", column2 = "Carrot" }
这是有意的。现在我想从DataTable中选择那些不同的值列分组符合数据表,然后获取Action | Quantity的唯一值。
因此,对于Fruit | Apple的分组,我会返回Sales | 1 Sales | 2的两行。
或者,如果有一种非常聪明的方法可以在不做第二次预告的情况下做到这一点,那就太棒了。
如何根据Type | Product上的分组从Action | Quantity列中获取唯一值?
答案 0 :(得分:0)
在Linq中使用Group by时,您可以将基础项目作为Enumberable。您需要做的就是另一个选择以获得您想要的值
var rptTable = from t in ReportConfigurationTable.AsEnumerable()
select new {Type = t["Type"], Product = t["Product"], Action = t["Action"], Quantity = t["Quantity"]};
var tableColumnGrouping = from table in rptTable
group table by new { table.Type, table.Product }
into groupedTable
select new
{
tableColumn = groupedTable.Key,
tableColumnCount = groupedTable.Count(),
actions = groupedTable.Select(t => new {Action = t.Action, Quantity = t.Quantity})
};