C#-将对象从列表框中添加到另一个
作者:互联网
我的问题如下:我有两个ListBoxes.在第一个中,我存储可以订购的产品,第二个是客户的购物车.因此,产品具有数量属性,而我具有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;
}
这样做很好,除了它向我的“产品”表中添加了新项目.我想做的是将选定数量的选定产品添加到客户购物车中,而不是向产品表中添加新项目,并使用numericupdown1.value减少订购的产品数量.我正在使用EF Code First数据库,如下所示:
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相同的数量.
解决方法:
您将同一实体(产品)用于两个不同的目的(产品和订单商品),这是错误的.
您看到的第一个行为是为任何订单创建新的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
}
您是否也注意到了更清洁的代码?
标签:ef-code-first,listbox,c,database 来源: https://codeday.me/bug/20191123/2067052.html