vb.net - looping through data in DAL -
vb.net - looping through data in DAL -
in dal loops through each in datatable applies object class. want able before passed object class check if row passed has same product code. example;
current row of info is;
productcode quantity 100001 500
if previous line of info passed has same product code;
productcode quantity 100001 500
it add together quantity previous line create 1000.
if not same productcode apply object class normal
so want running total in datatable
? utilize linq:
var runningtotalspercode = table.asenumerable() .select(row => new { productcode = row.field<int>("productcode"), quantity = row.field<int>("quantity"), allfields = row.itemarray }) .groupby(x => x.productcode) // grouping product-code .selectmany(g => g. // flatten grouping after running-total calculated select((x, index) => new { x.productcode, x.quantity, x.allfields, runningtotal = g.take(index + 1).sum(xx => xx.quantity) }));
then can loop create new datatable
or pass values object.
edit: whoops, noticed want vb.net. give me minutes....
dim codegroups = row in table allow productcode = row.field(of int32)("productcode") allow quantity = row.field(of int32)("quantity") allow code = new {productcode, quantity, .allfields = row.itemarray} grouping code code.productcode codegroup = grouping dim runningtotalspercode = codegroups. selectmany(function(g) g.codegroup.select(function(x, index) new { g.productcode, x.quantity, x.allfields, .runningtotal = g.codegroup.take(index + 1).sum(function(xx) xx.quantity) }))
quick test sample data:
dim table new datatable() table.columns.add("productcode", gettype(int32)) table.columns.add("quantity", gettype(int32)) table.rows.add(555555, 777) ' other grouping table.rows.add(100001, 500) table.rows.add(100001, 444) table.rows.add(100001, 442) productcode=555555, quantity=777, allfields={length=2}, runningtotal=777 <anonymous type> productcode=100001, quantity=500, allfields={length=2}, runningtotal=500 <anonymous type> productcode=100001, quantity=444, allfields={length=2}, runningtotal=944 <anonymous type> productcode=100001, quantity=442, allfields={length=2}, runningtotal=1386 <anonymous type>
vb.net
Comments
Post a Comment