C# 按C中数据表中的计数分组#
我是linq的新手 我有一个datatable,需要使用linq根据另一列更新一列 我的价值C# 按C中数据表中的计数分组#,c#,.net,linq,c#-4.0,C#,.net,Linq,C# 4.0,我是linq的新手 我有一个datatable,需要使用linq根据另一列更新一列 我的价值 customer | value1 | value2 | count ---------------------------------------- A | sda | sdas | 0 A | sda | sdas | 0 B | sda | sdas | 0 B | sda
customer | value1 | value2 | count
----------------------------------------
A | sda | sdas | 0
A | sda | sdas | 0
B | sda | sdas | 0
B | sda | sdas | 0
B | sda | sdas | 0
C | sda | sdas | 0
期望值
customer | value1 | value2 | count
----------------------------------------
A | sda | sdas | 2
A | sda | sdas | 2
B | sda | sdas | 3
B | sda | sdas | 3
B | sda | sdas | 3
C | sda | sdas | 1
您能为我推荐上述数据表所需的linq吗?您没有提供您的代码,但我认为您正在寻找一个类似这样的文件:(假设您的数据表名为“dt”) 您可以遵循的另一种方法是
GroupBy
datatable中的多列
dt.AsEnumerable()
.GroupBy(g => new {Customer = g["customer"], Value1 = g["value1"], Value2= g["value2"]})
.Select(g=>
{
var newRow = dtNew.NewRow();
row["customer"] = g.Key.Customer;
row["value1"] = g.Key.Value1;
row["value2"] = g.Key.Value2;
row["count"] = g.Count();
return newRow;
})
.CopyToDataTable();
请参阅:到目前为止,您尝试了什么?请向我们展示您的代码并解释您在哪里卡住了。
我有一个数据表,需要使用linq根据另一列更新一列
如何填充此数据表
,它是否绑定到源?你能更新你的帖子,包括你尝试过的和不起作用的吗?
dt.AsEnumerable()
.GroupBy(g => new {Customer = g["customer"], Value1 = g["value1"], Value2= g["value2"]})
.Select(g=>
{
var newRow = dtNew.NewRow();
row["customer"] = g.Key.Customer;
row["value1"] = g.Key.Value1;
row["value2"] = g.Key.Value2;
row["count"] = g.Count();
return newRow;
})
.CopyToDataTable();