将对象从列表框添加到另一个

时间:2013-04-29 18:31:44

标签: c# database listbox ef-code-first

我的问题如下:我有两个ListBoxe。首先,我存储可以订购的产品,第二个是客户的购物车。因此,产品具有Quantity属性,并且我有一个NumericUpDown,客户可以在其中更改订购产品的数量。

private void button2_Click(object sender, EventArgs e) //puts selectedproduct to the cart
{
    if (listBox1.SelectedItem == null || (int)numericUpDown1.Value == 0 ||
        (listBox1.SelectedItem as Product).Quantity < (int)numericUpDown1.Value) return;

    foreach (Product item in listBox2.Items)
    {
        if (item.ID == (listBox1.SelectedItem as Product).ID)
        {
            return;
        }
    }

    listBox2.Items.Add(new Product { Name = (listBox1.SelectedItem as Product).Name,
                                 Price = (listBox1.SelectedItem as Product).Price,
                                 Quantity = (int)numericUpDown1.Value});

    (listBox1.SelectedItem as Product).Quantity -= (int)numericUpDown1.Value;
}

这很好用,除了它在我的Products表中添加了一个新项目。我想要做的是向客户添加所选产品的选定数量,而不是向产品表添加新商品,并使用numericupdown1.value减少订购的产品数量。我正在使用EF Code First Database,如下所示:

public class Order
{
    public int ID { get; set; }
    public bool Status { get; set; }

    public virtual Account account { get; set; }
    public virtual List<Product> products { get; set; }        
}

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public long Price { get; set; }
    public int Quantity { get; set; }

    public virtual List<Order> orders { get; set; }       
}

以下是保存方法:

private void button1_Click(object sender, EventArgs e) 
{
    List<Product> p = new List<Product>();
    foreach (Product item in listBox2.Items)
    {
        p.Add(item);
    }
    Variables.Db.orders.Add(new Order { account = Variables.Currentuser, products = p });
    Variables.Db.SaveChanges(); //Variables.Db is my CodeFirst Database
}

我尝试了很多方法,例如:

int q = (listBox1.SelectedItem as Product).Quantity;
(listBox1.SelectedItem as Product).Quantity = (int)numericUpDown1.Value;
listBox2.Items.Add(listBox1.SelectedItem);

(listBox1.SelectedItem as Product).Quantity = q - (int)numericUpDown1.Value;

但最后一行将listbox2对象的数量更改为与listbox1s相同。

1 个答案:

答案 0 :(得分:3)

您使用相同的实体(Product)用于两个不同的目的(产品和订单商品),这是错误的。

您看到的第一个行为是为任何订单创建新Product个对象的结果,第二个行为是因为您在两个列表框中使用相同的产品对象。

执行此操作的正确方法是为订单商品创建新实体:

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public long Price { get; set; }
    public int Quantity { get; set; }

    public virtual List<OrderItem> ordersItems { get; set; }
}

public class Order
{
    public int ID { get; set; }
    public bool Status { get; set; }

    public virtual Account account { get; set; }
    public virtual List<OrderItem> Items { get; set; }        
}

public class OrderItem
{
    public int ID { get; set; }
    public int Quantity { get; set; }
    public virtual Product Product { get; set; }
    public long TotalPrice { get; set; } // It is better to hold the price. What if you later changed the product price?
}

然后您可以轻松处理购买:

private void button2_Click(object sender, EventArgs e) //puts selectedproduct to the cart
{
    Product p = listBox1.SelectedItem as Product;
    int q = (int)numericUpDown1.Value;
    if (p == null || q == 0 || p.Quantity < q)
        return;

    foreach (Product item in listBox2.Items)
    {
        if (item.ID == p.ID)
        {
            return;
        }
    }

    listBox2.Items.Add(new OrderItem { Product = p, Quantity = q, TotalPrice = p.Price * q });
    p.Quantity -= q;
}


private void button1_Click(object sender, EventArgs e) 
{
    Order o = new Order() { account = Variables.Currentuser, Items = new List<Order>() };
    foreach (OrderItem item in listBox2.Items)
    {
        o.Items.Add(item);
    }
    Variables.Db.orders.Add(o);
    Variables.Db.SaveChanges(); //Variables.Db is my CodeFirst Database
}

您是否也注意到清洁代码? :)

还有一件事:请始终为控件和类字段使用有意义的名称:btnSavebtnAddToCartnumQuantity是不错的候选人。