這可能看起來很傻,但所有的例子,我發現在使用LINQ使用Except
兩個列表或僅字符串或整數數組和過濾他們根據比賽,例如:過濾器的LINQ除了在性能
var excludes = users.Except(matches);
我想用排除,以保持我的代碼短而簡單,但似乎無法找出如何做到以下幾點:
class AppMeta
{
public int Id { get; set; }
}
var excludedAppIds = new List<int> {2, 3, 5, 6};
var unfilteredApps = new List<AppMeta>
{
new AppMeta {Id = 1},
new AppMeta {Id = 2},
new AppMeta {Id = 3},
new AppMeta {Id = 4},
new AppMeta {Id = 5}
}
我怎樣可以過濾對AppMeta
回列表excludedAppIds
?
爲了提高效率,建議存儲'excludedAppIds'作爲'HashSet',否則你有一個'O(N²)'算法遍歷你的排除列表的次數與源代碼中的元素一樣多。 – 2017-08-08 11:23:17