开发者

fastest way to see if IEnumerable<T> contains item based on criteria

开发者 https://www.devze.com 2023-01-16 21:59 出处:网络
Sort of a Linq beginners question, but is there a simple built-in way to optimize this: bool cont开发者_如何转开发ainsItemWithValue42 = items.Where(i => i.Value == 42).Count() > 0;

Sort of a Linq beginners question, but is there a simple built-in way to optimize this:

bool cont开发者_如何转开发ainsItemWithValue42 = items.Where(i => i.Value == 42).Count() > 0;

I would like Linq to stop iterating as soon as it found a match.


The Any method does exactly that:

bool containsItemWithValue42 = items.Any(i => i.Value == 42);
0

精彩评论

暂无评论...
验证码 换一张
取 消