Linq。Any VS。Exists - 有什么区别? [英] Linq .Any VS .Exists - Whats the difference?

查看:715
本文介绍了Linq。Any VS。Exists - 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在集合上使用Linq,以下代码行之间有什么区别?

Using Linq on collections, what is the difference between the following lines of code?

if(!coll.Any(i => i.Value))

if(!coll.Exists(i => i.Value))

更新1

当我反汇编.Exists时,看起来没有代码。

When I disassemble .Exists it looks like there is no code.

更新2

任何人都知道为什么没有代码吗?

Anyone know why there is not code there for this one?

推荐答案

查看文档

List.Exists (对象方法 - MSDN)

List.Exists (Object method - MSDN)


确定List(T)是否包含与指定谓词定义的条件匹配的元素。

Determines whether the List(T) contains elements that match the conditions defined by the specified predicate.

.NET 2.0,所以在LINQ之前。希望与谓词委托配合使用,但lambda表达式向后兼容。此外,只有List有这个(甚至IList)

This exists since .NET 2.0, so before LINQ. Meant to be used with the Predicate delegate, but lambda expressions are backward compatible. Also, just List has this (not even IList)

IEnumerable.Any (扩展方法 - MSDN)

IEnumerable.Any (扩展方法 - 堆栈溢出文档)

IEnumerable.Any (Extension method - MSDN)
IEnumerable.Any (Extension method - Stack Overflow Docs)


确定序列的任何元素是否满足条件。

Determines whether any element of a sequence satisfies a condition.

这是.NET 3.5中的新增功能, bool)作为参数,所以这意味着用于lambda表达式和LINQ。

This is new in .NET 3.5 and uses Func(TSource, bool) as argument, so this was intended to be used with lambda expressions and LINQ.

在行为中,这些是相同的。

In behaviour, these are identical.

这篇关于Linq。Any VS。Exists - 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆