0

我有以下 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)
                       }

我有这个名为 dx 的数据表

catalogid      backorder
1              5
1              10
2              1
2              5

我想Sum backorder列在哪里catalogid是相同的所以结果如下

catalogid      backorder
1              15
2              6

在 Select new with 部分中,有什么问题?

4

1 回答 1

1

尝试跟随...

 var result = dx.AsEnumerable()
                .GroupBy(row => row.Field<int>("catalogid"))
                .Select(group => group.Sum(item => item.Field<int>     ("backorder")).CopyToDataTable();

或者

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(); 
于 2012-10-27T09:42:28.190 回答