【发布时间】:2013-08-30 16:56:19
【问题描述】:
这是我的数据表
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。
或者,如果有一种非常聪明的方法可以做到这一点而无需进行第二次 foreach,那就太棒了。
如何根据类型|产品的分组从操作|数量列中获取唯一值?
【问题讨论】:
-
我还不清楚你想要什么,我认为你应该更新详细的示例输出。
-
这里很简单,所以对于 Fruit|Apple 的分组,我将返回两行 Sales|1 Sales|2
-
我同意这个问题不清楚。看来您应该使用
Distinct方法并使用 lambda 语句指定您要应用不同的值,但我不想写答案,因为我不太确定。 -
所以对于 Fruit|Apple 的分组,我将返回两行 Sales|1 Sales|2