字符串和值重复列表

时间:2015-05-15 12:34:42

标签: c# database list

我有db实体orderItems和类OrderCountet 我有方法计数基于orderItems统计我店里最受欢迎的产品。

在下面的代码后,我有这样的输出:

Name          Quantity
Chocapic       7
Bread          2
Chocapic       7
Bread          2
Cheese         3

我想要制作此列表而不重复。 因为现在输出说我在这个列表中有14个chocapic但我只有7个。 我试图搜索如何实现这一点,但我只找到了HashSet。 也是不同的List解决方案,但仅适用于String或Integer的列表。

    Public class Statistic
{

    //ProductName
    String Name {get: set;} 
    //quantity product
    Int quantity {get: set;}
       //class for counting
    and 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;
    }
}

enter image description here

3 个答案:

答案 0 :(得分:2)

这是因为你继续在列表中添加相同的对象:temp在循环之前创建一次,并且多次添加。

在循环中移动此行以解决问题:

Statistic temp =new Statistic();

如果可以,请为Statistics提供一个带namequantity的构造函数,并通过将其setter的可访问性更改为private来使该对象不可变。

答案 1 :(得分:1)

您可以尝试在foreach方法

中使用以下Counting()循环吗?
        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);
                }
            }
        }

希望这会有所帮助......

答案 2 :(得分:0)

这是有效的:)

public void Counting()
        {
            foreach (Order k in db.OrderItems)
            {
                int sum = 0;
                Statystyki temp = new Statystyki();
                foreach (Product product in db.Products)
                {

                    if (product.Id == k.productId)
                    {
                        sum += k.quantity;
                    }
                }

                temp.Nazwa = k.Product.Name;
                if (!lista_statystyk.Exists(x => string.Compare(x.Nazwa, temp.Nazwa, true) == 0))
                {

                    temp.suma = sum;
                    lista_statystyk.Add(temp);
                }
                else if (lista_statystyk.Exists(x => string.Compare(x.Nazwa, temp.Nazwa, true) == 0))
                {
                    temp.suma += sum;
                    lista_statystyk.Add(temp);
                }
            }
        }