有什麼辦法可以將以下代碼縮減爲Linq格式?將2個foreach循環縮減爲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
爲什麼要在'LINQ'中使用這個特殊原因?它可能不會優化代碼,並且會降低可讀性。 – LukeHennerley 2013-03-25 11:41:29
「LINQ規則#1」:除非您能夠在3分鐘內自行提供LINQ查詢,否則不值得使用它。 :) – JleruOHeP 2013-03-25 11:42:18
編輯到LINQ規則#1,如果你的職業....我不以任何方式塑造或形成Linq的專家,所以這條規則不適用。 – 2013-03-25 12:57:23