使用SQL LINQ更新表(缓慢执行)

时间:2016-11-06 23:39:04

标签: c# linq

以下代码有效。但是,我认为我做错了因为它运行缓慢。我正在遍历客户记录列表,以确定是否有任何更改,或者是否添加了任何内容。我正在使用LINQ,以及两个FOREACH循环。需要第一个FOREACH,但第二个只是搜索要验证的记录(10到20个记录)。我错过了搜索方法吗?

  var ResultSKUConnection = from a in SKUConnections
                            where ((a.Customer_ID == Cust_ID) && (a.Connection_Name == "eBay") && (a.Item_ID == item.ItemID))
                            select a;
  foreach (VariationType varitem in item.Variations.Variation)
  {
     Boolean found = false;
     foreach (SKUConnection SKUitem in ResultSKUConnection)
     {
        if (SKUitem.SKU==varitem.SKU)
        {
           found = true;
           Console.WriteLine($"Found  Item ID {item.ItemID}, SKU {varitem.SKU}");
           if (SKUitem.eBay_Price != varitem.StartPrice.Value || SKUitem.eBay_Price_Currency != varitem.StartPrice.currencyID.ToString())
           {
              SKUitem.eBay_Price = varitem.StartPrice.Value;
              SKUitem.eBay_Price_Currency = varitem.StartPrice.currencyID.ToString();
              Console.WriteLine($"UPDATE  Item ID {item.ItemID}, SKU {varitem.SKU}");
           }
           break;
        }
     }
     if (!found)
     {
        SKUConnection NewSKU = new SKUConnection();
        NewSKU.SKU = varitem.SKU;
        NewSKU.eBay_Price = varitem.StartPrice.Value;
        NewSKU.eBay_Price_Currency = varitem.StartPrice.currencyID.ToString();
        NewSKU.eBay_VariationSpecifics = JsonConvert.SerializeObject(varitem.VariationSpecifics);
        SKUConnections.InsertOnSubmit(NewSKU);
        Console.WriteLine($"ADD  Item ID {item.ItemID}, SKU {varitem.SKU}");
     }
  }
  db.SubmitChanges();

0 个答案:

没有答案