【发布时间】:2017-02-02 12:09:50
【问题描述】:
我写了如下代码:
foreach (var itemA in itm)
{
foreach (var itemB in filteredList)
{
if (itemA.ItemID != itemB.ItemID)
{
missingList.Add(itemB);
ListToUpdate.Add(itemB);
}
else
{
if (itemA.QuantitySold != itemB.QuantitySold)
{
ListToUpdate.Add(itemB);
}
}
}
}
如您所见,我在这里有两个列表,它们的结构相同,它们是:
List #1 is "itm" list - which contains old records from DB
List #2 is "filteredList" - which has all items from DB and + new ones
我正在尝试根据下一个条件将项目添加到 missingList 和 ListToUpdate:
All items that are "new" in filteredList - meaning their ItemID doens't exists in "itm" list should be added to missingList.
And all items that are new in filteredList- filteredList - meaning their ItemID doens't exists in "itm" list should be added to .ListToUpdate
And final criteria to add items to ListToUpdate should be those items that exist in both lists - and if the quantitysold in "itm" list is different - add them to ListToUpdate
我写的上面的代码给了我完全错误的结果,我最终在两个列表中都有超过 50000 个额外的项目......
我想按照我上面写的那样工作的方式更改此代码,并可能使用并行循环或 PLINQ 来加快速度...
有人可以帮帮我吗?
【问题讨论】:
-
简而言之,您有两个列表,为仅存在于过滤列表中的 ID 创建一个新列表,并为两个列表中都存在的 ID 创建另一个列表 (ListToUpdate)。对吗?
-
您应该为每个标准编写一个方法来接收问题的 3 个子集,然后组合结果。奖励:这 3 种方法可以并行运行。
-
@Anand 是的,这是正确的,对第二个列表稍作更改 - 为两个列表中都存在的 ID 创建另一个列表(ListToUpdate),但只有那些具有不同 QuantitySold 属性的 ID,这意味着 ItemID 来自新列表的销售数量多于旧列表中的 ItemID (db)
-
@Dawnkeeper 你能给我举个例子吗?
标签: c# list c#-4.0 parallel-processing compare