Insert valve from listview to Sql (using linq to sql)

混江龙づ霸主 提交于 2019-12-25 02:26:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!