我有一个大的IEnumerable of EntityObjects和一个大的字符串IEnumerable,它们是对象的一个键。
我只想获得匹配键的对象的新列表。目前,我正在通过Contains()来做这件事--但是它看起来很慢吗?
class Foo {
string Key
string Prop1
int Prop2
decimal Prop3
Bar Prop4
Thing Prop5
Stuff Prop6
...more properties
}
IEnumerable<Foo> foos
IEnumerable<string> fooKeys
var matchedFoos = foos.Where(f => fooKeys.Contains(f.Key));这是工作,并返回了我的期望,但似乎是缓慢,我认为一定有更好的方法?我见过一些互联网络上的帖子,但似乎是针对同一类型的可枚举类的吗?
有关信息:
foos.Count()约164,000fooKeys.Count()约75,000发布于 2013-12-30 06:05:13
fooKeys更改为HashSet<string> (如果不是),使Contains()方法调用O(1)而不是O(n):
keesSet =新HashSet(fooKeys);var matchedFoos = foos.Where(f => keesSet.Contains(f.Key));
但是有了这么大的收藏,它仍然需要相当多的时间来执行搜索。发布于 2013-12-30 06:20:20
我认为还有一个类似于联接条款的变体
IEnumerable<Foo> foos
IEnumerable<string> fooKeys
var matchedFoos = from foo in foos
join fk in fooKeys on foo.Key equals fk
select foo;https://stackoverflow.com/questions/20834189
复制相似问题