列表任何还是计数?

发布于 2024-11-02 19:26:05 字数 194 浏览 0 评论 0原文

当我想对列表执行某些操作时,我首先检查它是否不为 null 或不包含任何元素(不要破坏 foreach),我通常使用 list .Any() 但最好的选择是什么 - 使用 list.Count > 0 ,还是使用 list.Any()

When I want to do something with a list I first check it if is not null or contains no elements (not to blow a foreach) and I usually use list.Any() but what is the best option - to use list.Count > 0 , or to use list.Any()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

复古式 2024-11-09 19:26:05
  • 如果您使用的是List,请使用Count,因为它知道其大小。
  • Array 使用 Length
  • 如果您只有一个 IEnumerable,我会使用 .Any() 而不是 .Count() 因为它会更快,因为它在检查一项后停止。

另请查看这个问题:哪种方法性能更好:.Any() vs .Count() > 0?

  • Use Count if you're using a List, since it knows its size.
  • Use Length for an Array
  • If you just have an IEnumerable I would use .Any() over .Count() as it will be faster since it stops after checking one item.

Also check out this question: Which method performs better: .Any() vs .Count() > 0?

凡尘雨 2024-11-09 19:26:05

我使用 list.Count > 0 只是因为它不依赖于 LINQ 方法,因此适用于 C# 2.0。

我个人像躲避瘟疫一样避开 LINQ(因为它的速度慢),而且无论如何也没有理由在这里使用扩展方法。

但是,更好的解决方案可能是制作您自己的 Any 版本,该版本将接受 null 引用,如果是包含元素的集合则返回 true。这将为您节省空检查。

I use list.Count > 0 just because it doesn't depend on the LINQ methods and so works on C# 2.0.

I personally avoid LINQ like the plague (because of its slow speed), and there's no reason to use extension methods here at all anyway.

However, a better solution would probably be to make your own version of Any that would take in a null reference, and return true if it's a collection with elements. That would save you the null check.

征棹 2024-11-09 19:26:05

.Any() 通常比 .Count() > 更好用0 。这样做的原因是,如果您要迭代的项目不是 ICollection,那么它将必须迭代整个列表才能获取计数。

但是,如果项目是一个 ICollectionList 就是这样),那么使用 Count()< 的速度同样快,或者在某些情况下更快。 /code> (Any() 迭代一次,无论 MS .Net 中的底层类型如何,但当底层项是 时,Mono 会尝试将其优化为 Count > 0 >ICollection)

Reflector 是一个很棒的工具,.Net 源代码和 < a href="https://github.com/mono/mono/blob/master/mcs/class/System.Core/System.Linq/Enumerable.cs" rel="nofollow noreferrer">单声道源代码让您可以看到事情是如何实现的。

.Any() is generally better to use than .Count() > 0. The reason for this is that if the items you are iterating over is not an ICollection then it will have to iterate the whole list to get the count.

But if the items is an ICollection (which a List<T> is) then it is just as fast or in some cases faster to use Count() (Any() iterates once regardless of underlying type in MS .Net but Mono tries to optimize this to Count > 0 when the underlying items is an ICollection)

A great tool is Reflector, the .Net source code and the Mono source code which allows you to see how things are implemented.

断爱 2024-11-09 19:26:05

如果您使用实体框架并且有一个包含许多记录的巨大表,Any() 会快得多。我记得有一次我想检查一张表是否为空并且它有数百万行。 Count() > 花费了 20-30 秒0 完成。 Any() 是即时的。

If you are using the Entity Framework and have a huge table with many records Any() will be much faster. I remember one time I wanted to check to see if a table was empty and it had millions of rows. It took 20-30 seconds for Count() > 0 to complete. It was instant with Any().

秉烛思 2024-11-09 19:26:05

Any() 可以提高性能,因为它可能不必迭代集合来获取事物的数量。只要击中其中一个就可以了。或者,对于 LINQ-to-Entities,生成的 SQL 将是 IF EXISTS(...) 而不是 SELECT COUNT ... 甚至 SELECT * ...。

Any() can be a performance enhancement because it may not have to iterate the collection to get the number of things. It just has to hit one of them. Or, for, say, LINQ-to-Entities, the generated SQL will be IF EXISTS(...) rather than SELECT COUNT ... or even SELECT * ....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文