小编sam*_*gua的帖子

如何有效地用另一个列表更新列表C#

我有两个包含对象元素的列表,一个大列表称为List1,另一个小列表称为List2。我需要基于在函数中定义的条件,用List2中的值更新List1中的值,该函数基于对象中的值返回布尔值。我想出了以下实现,对于较大的列表,这确实需要很多时间。

检查项目是否将被更新的功能

private static bool CheckMatch(Item item1, Item item2) { 
//do some stuff here and return a boolean
}
Run Code Online (Sandbox Code Playgroud)

查询我用来更新物品

在下面的代码段中,我需要使用List2(小列表)中的一些值更新List1(大列表)

    foreach(var item1 in List1)
    {
        var matchingItems = List2.Where(item2 => CheckMatch(item1, item2));
        if (matchingItems.Any())
        {
            item1.IsExclude = matchingItems.First().IsExcluded;
            item1.IsInclude = matchingItems.First().IsIncluded;
            item1.Category = matchingItems.First().Category;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我希望我能得到一个比这更好的解决方案。我还需要保持元素在List1中的位置

这是我在做 什么的示例这是我在做什么的示例

c# linq big-o list

-1
推荐指数
1
解决办法
192
查看次数

标签 统计

big-o ×1

c# ×1

linq ×1

list ×1