问题
i have three different Listview which is Item,quantity and price. All the items for the listview will be added from a textbox. I would like to know how can i store the listview items into my SQl database. Here is the code i use to add item into the listview.
private void SalesAddItemBtn_Click(object sender, RoutedEventArgs e)
{
ListProductName.Items.Add(ComSalesProduct.Text);
ListProductQuantity.Items.Add(txtSalesQuantity.Text);
ListProductCost.Items.Add("RM " + txtSalesCost.Text+".00");
}
Thanks in advance..
回答1:
Assuming your entity is defined something like this:
public class Order
{
public string product;
public string quantity;
public string cost;
}
You can Zip to combine the lists and update the database:
var orders = ListProductName.Zip(ListProductQuantity, (p, q) => new { Product = p, Quantity = q }).Zip(
ListProductCost, (p, c) => new Order() { product = p.Product, quantity = p.Quantity, cost = c });
db.Orders.InsertAllOnSubmit(orders);
db.SubmitChanges();
来源:https://stackoverflow.com/questions/9928026/insert-valve-from-listview-to-sql-using-linq-to-sql