更新匹配的对象值

时间:2018-08-25 04:19:34

标签: c#

检查下面的代码。我有一些数据的oldPricenewPrice变量。然后,我将它们一起添加到一个名为-allItems的列表中。现在,您会注意到变量oldPricenewPrice都具有ASIN属性,该属性是每个数据集的标识。现在,我的目标是-在allItems变量中,我想更新匹配的ASIN值数据price。像ASIN = “one”, ASIN = “two”newPriceoldPrice中都是常见的,因此应从newPriceoldPriceone and two中进行更新。我怎样才能做到这一点?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASIN_REFRESH_2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Test App");

            //old price has 4 values
            var oldPrice = new List<ThirdPartyData>();
            oldPrice.Add(new ThirdPartyData { ASIN = "one", price = 5, IsPrime = true });
            oldPrice.Add(new ThirdPartyData { ASIN = "two", price = 6, IsPrime = true });
            oldPrice.Add(new ThirdPartyData { ASIN = "three", price = 7, IsPrime = false });
            oldPrice.Add(new ThirdPartyData { ASIN = "four", price = 8, IsPrime = false });

            //new price has 2 values
            var newPrices = new List<ThirdPartyData>();
            newPrices.Add(new ThirdPartyData { ASIN = "one", price = 1, IsPrime = false });
            newPrices.Add(new ThirdPartyData { ASIN = "two", price = 2, IsPrime = true });

            var allItems = new List<ThirdPartyData>();//here old price and new price added in single list
            allItems.AddRange(newPrices);
            allItems.AddRange(oldPrice);

            Console.ReadKey();
        }
    }

    class ThirdPartyData
    {
        public string ASIN { get; set; }
        public int price { get; set; }
        public bool IsPrime { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

您的要求不明确(至少对我而言),但是这里是您开始基于另一个列表更新列表的方法:

UPDATE asbuilt_list
INNER JOIN tbl_transmittal_discipline_poi_max ON asbuilt_list.doc = tbl_transmittal_discipline_poi_max.[owner document number]
SET asbuilt_list.commented_sheet =
       Nz((SELECT [pages]
           FROM [tbl_transmittal_discipline_poi_max] AS T1
           WHERE (T1.[owner document number] = asbuilt_list.[doc])
             AND (T1.poi = "cmn" )), 0)
WHERE 
    (((asbuilt_list.description ) NOT LIKE "*isometric*")
     AND ((asbuilt_list.doc) NOT LIKE "*ls*")
     AND ((asbuilt_list.discipline) <> "ppd"))
      OR ((( sbuilt_list.description) IS NULL)
          AND ((asbuilt_list.discipline) IS NULL));  

为什么要地图?您的示例的列表很小,但是如果旧列表很大(数千个项目)怎么办?您必须查找匹配项,这会在常规列表中使用O(n)。该映射有助于将其减少为O(1)。