我有一个对象集合,想要检索出所有具有某个属性匹配搜索字符串的对象。到目前为止,我已经尝试了几种过滤方法,包括List.ForAll、IEnumerable.Where和ParallelQuery.Where。
List<Foo> cache = GetAllObjs(); // source list containing lots of objects
选项1:
List<Foo> foos = cache.AsParallel().Where(x => x.Name == "bar").ToList();
选项2:
List<Foo> foos = cache.Where(x => x.Name == "bar").ToList();
选项三:
List<Foo> foos = cache.FindAll(x => x.Name == "bar");
由于ParallelQuery.Where利用了多个核心,因此它似乎是最快的解决方案。除此之外,还有其他过滤方法,例如使用不同的集合类型或过滤函数吗?源集合不必是List。
ToList
也可能是不必要的。 - Sayse