Select distinct rows and sum columns in DataTable VB.Net 2005 -
i have datatable contains:
id pocount pototal
1 10
2 20
b 4 10
i want result of new data table bellow:
id pocount pototal
3 30
b 4 10
how can using datatable? project in vb.net 2005 , cannot use linq method. best way this?
i found link kinda near want. skip rows instead of summing columns when id similar. http://www.dotnetfunda.com/forums/show/2603/how-to-remove-duplicate-records-from-a-datatable
linq better - upgrade later vs express - free!
here 1 approach using class , dictionery
public class posummary public property id string public property count integer public property total integer sub new(poid string, pocount integer, pototal integer) id = poid count = pocount total = pototal end sub end class private sub button12_click(sender object, e eventargs) handles button12.click dim pos new list(of posummary) dim po new posummary("a", 1, 10) pos.add(po) po = new posummary("a", 2, 20) pos.add(po) po = new posummary("b", 4, 10) pos.add(po) debug.print("--data--") each p posummary in pos debug.print(p.id & " " & p.count & " " & p.total) next dim pd new dictionary(of string, posummary) each p posummary in pos if not pd.containskey(p.id) pd.add(p.id, p) else pd(p.id).count += p.count pd(p.id).total += p.total end if next debug.print("--totals--") each kvp keyvaluepair(of string, posummary) in pd po = kvp.value debug.print(po.id & " " & po.count & " " & po.total) next stop end sub
Comments
Post a Comment