c# - Grouping and Sum DataTable Rows without Linq -
c# - Grouping and Sum DataTable Rows without Linq -
i have datatable this
name cost product1 10 product1 12 product2 3 product2 5 product2 7 product3 22
i grouping same named products , sum prices --->
name cost product1 10 product1 12 22 product2 3 product2 5 product2 7 15 product3 22 22
i should't create linq. how can distinguish different value of names , sum them in table.
you dictionary key name of product, , value sum of same product prices.
var allproducts = context.products.where(.....what products want take db).tolist(); dictionary<string, int32> dictionary = new dictionary<string, int32>(); foreach(var product in allproducts) { int32 value = 0; if(!dictionary.trygetvalue(product.name, out value)) { dictionary.add(product.name, product.price); continue; } value += product.price; dictionary[product.name] = value; }
c# asp.net datatable
Comments
Post a Comment