LINQ的。任何VS .Exists - 请告诉我有什么区别? [英] Linq .Any VS .Exists - Whats the difference?

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

问题描述

在集合使用LINQ,是code?

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它看起来像没有code。

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

更新2

任何人都知道为什么没有code还有这一个?

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

推荐答案

请参阅文件

List.Exists (对象方法 - MSDN)

List.Exists (Object method - MSDN)

确定列表(T)是否包含匹配指定predicate定义的条件的元素

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

这存在,因为.NET 2.0,所以LINQ之前。为了与predicate 代表可以使用,但拉姆达前pressions向后兼容。此外,只列出有这个(甚至没有的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的新使用Func键(TSource,布尔)作为参数,所以这是旨在与拉姆达前pressions和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的。任何VS .Exists - 请告诉我有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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