Take the tour ×

i have the following vb.net LINQ Query

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

catalogid      backorder
1              5
1              10
2              1
2              5

i want to Sum backorder column where catalogid is the same so the result is the following

catalogid      backorder
1              15
2              6

in the Select new with part, what is wrong?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Try following...

 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(); 
share|improve this answer
any hint on how can i put the result in a datatable or save it to the dx datatable? –  user1570048 Oct 27 '12 at 10:09
Use CopyToDataTable(),see my editted answer –  Amol Kolekar Oct 27 '12 at 10:19
except copyToDataTable wont work with Enumerable –  user1570048 Oct 27 '12 at 16:59
add comment

Your Answer


