c# - List of String and Value Duplicates -
i have db entity orderitems , class ordercountet have method counting made statistic of popular products in shop based on orderitems.
after code below have output this:
name quantity chocapic 7 bread 2 chocapic 7 bread 2 cheese 3
i want make list without duplicates. because output says have 14 chocapic in list have 7. trying search how achieve found hashset. distinct list solution list of string or integer.
public class statistic { //productname string name {get: set;} //quantity product int quantity {get: set;} //class counting , ordercounter ordercounter{get: set;} } public class ordercounter { private list<statistic> list_statistic =new list<statistic>(); public list<statistic> list_statistic { get{ return list_statistic ;} } public void counting() { foreach(order k in db.orderitems) { int sum=0; statistic temp =new statistic(); foreach(product product in db.products) { if(product.id==k.productid { sum+=sum+k.quantity; } temp.quantity=sum; temp.name=k.product.name(); list_statistic.add(temp); } } if(orders.product.id==product.id) { sum+=suma +k.quantity; } }
could try using following foreach
loop in counting()
method,
foreach (order k in db.orderitems) { int sum = 0; foreach (product product in db.products) { statistic temp = new statistic(); if (product.id == k.productid) { sum += k.quantity; } temp.quantity = sum; temp.name = k.product.name(); if (!list_statistic.exists(x => string.compare(x.name, temp.name, true) == 0)) { list_statistic.add(temp); } } }
hope helps...
Comments
Post a Comment