LINQ .Any VS .Exists - 有什么区别? [英] LINQ .Any VS .Exists - What's the difference?

查看:48
本文介绍了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 no 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 之前.旨在与 Predicate delegate 一起使用,但 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 (Extension method - MSDN)

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

Determines whether any element of a sequence satisfies a condition.

这是 .NET 3.5 中的新功能,并使用 Func(TSource, 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天全站免登陆