C# 比较两个列表,仅当第一个列表中的条件与第二个列表中的条件匹配时才更新
我有两个列表(比如calList和checkerList)C# 比较两个列表,仅当第一个列表中的条件与第二个列表中的条件匹配时才更新,c#,linq,lambda,C#,Linq,Lambda,我有两个列表(比如calList和checkerList) 我需要检查checkerList上的条件是否满足 如果是,则更新该calList的项目值 最终的calList应该是完整的calList(无论是否更新) 我所尝试的: resultantCalList = calList .Where(item1 => checkerList.Any(item2 => item1.VendorFiel
resultantCalList = calList .Where(item1 =>
checkerList.Any(item2 =>
item1.VendorFieldId == item2.Id
&& item2.VendorId == vendorId
&& item2.Sensitive
))
.Select(s => {s.Value = 10; return s; })
.ToList();
这仅提供更新项的集合,即ResultCalist仅包含其值已更新的项的列表。没有匹配项的项目不会包含在ResultantCalist
中
resultantCalList =
calList.Select(item1 =>
{
if (checkerList.Any(item2 =>
item1.VendorFieldId == item2.Id
&& item2.VendorId == vendorId
&& item2.Sensitive))
{
item1.Value = 10;
}
return item1;
})
.ToList();
您能给我一个lambda查询,它提供了一个列表,其中包含calList中的所有项目,这些项目将同时具有更新值(如果找到匹配项)和原始值(如果未找到匹配项)?在
Select
语句中移动Where
子句将避免在calList返回ToList
之前过滤calList的内容
resultantCalList =
calList.Select(item1 =>
{
if (checkerList.Any(item2 =>
item1.VendorFieldId == item2.Id
&& item2.VendorId == vendorId
&& item2.Sensitive))
{
item1.Value = 10;
}
return item1;
})
.ToList();