有什么办法可以将下面的代码缩减成Linq形式吗?
foreach (var current in currentWhiteListApps)
{
var exists = false;
foreach (var whiteList in clientSideWhiteLists)
{
if (current.appID.Equals(whiteList.appID))
{
exists = true;
}
}
if (!exists)
{
deleteList.Add(current);
}
}
我能想到的是:
currentWhiteListApps.Select(x => {
var any = clientSideWhiteLists.Where(y => y.appID.Equals(x.appID));
if (any.Any())
deleteList.AddRange(any.ToArray());
return x;
});
使用 LINQ 的原因
LINQ
比嵌套的 foreach 循环更具可读性,并且需要的代码更少。所以这就是我希望在 LINQ
最佳答案
var deleteList = currentWhiteListApps.Where(x =>
clientSideWhiteLists.All(y => !x.appID.Equals(y.appID)))
.ToList();
关于c# - 将 2 个 foreach 循环减少到一个 linq 查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15613999/