vb.net LINQ Group by 和 Sum 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13098889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:55:07 来源:igfitidea点击:
LINQ Group by and Sum syntax
提问by user1570048
i have the following vb.net LINQ Query
我有以下 vb.net LINQ 查询
Dim q = From row In dx.AsEnumerable
Group row By G = New With {.Cat = row.Field(Of Integer)("catalogid")}.Cat,
New With {.Backorder = row.Field(Of Integer)("backorder")}.Backorder Into pg = Group
Select New With
{
.Cat = pg.Cat,
.Backorder = pg.Sum(Function(x) x.Backorder)
}
i have this datatable called dx
我有一个名为 dx 的数据表
catalogid backorder
1 5
1 10
2 1
2 5
i want to Sumbackordercolumn where catalogidis the same so the result is the following
我想Sumbackorder列 wherecatalogid是相同的所以结果如下
catalogid backorder
1 15
2 6
in the Select new with part, what is wrong?
在 Select new with 部分,有什么问题?
回答by Amol Kolekar
Try following...
尝试跟随...
var result = dx.AsEnumerable()
.GroupBy(row => row.Field<int>("catalogid"))
.Select(group => group.Sum(item => item.Field<int> ("backorder")).CopyToDataTable();
or
或者
var result= from s in dx.AsEnumerable()
group s by s.Field<int>("catalogid") into grp
select new {
CatalogId= grp.Key,
Back Order = grp.Sum(r => r.Field<int>("backorder"))
};
DataTable datatbl = result.CopyToDataTable();

