使用 LINQ 时,.Select、.Any 和 .Count 之间有什么区别
使用 LINQ 时,.Select、.Any 和 .Count 之间有什么区别 使用 .Count 时是否会像 SQL select count(*) 一样影响性能? .Any 执行得更快吗?
谢谢!
What are the difference between .Select, .Any, and .Count when using LINQ
Do you get a performance hit when using .Count just like in SQL select count(*)?
Does .Any perform faster ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Count
需要迭代整个集合,因为它(显然)需要计算实例的数量。Any
查找第一个匹配项并找到第一个匹配项返回 true 或 false。如果没有,那么它需要迭代整个集合来尝试查找,但如果第一个实例匹配,那么它只需要检查第一个实例。Select
完全不同。它用于将一个集合投影到另一个集合中。它不执行任何检查或过滤。编辑:在 SQL 术语中,
Any
就像Exists
,而Count
就像Count(*)
。如果我想知道今天街上有没有人,完全不需要数所有的人,看看是否>=1,只要找到一个人就完了。
Count
needs to iterate the entire collection, because it (obviously) needs to count the number of instances.Any
finds the first occurrence & returns true or false. If there aren't any, then it needs to iterate the whole collection to try to find, but if the first instance matches then it only needs to check the first instance.Select
is completely different. It is used to project a collection into another collection. It does not perform any checking or filtering.edit: In SQL terms,
Any
is likeExists
, whileCount
is likeCount(*)
.If I want to know whether there are any people on the street today, it is completely unnecessarily to count all the people and see if the number is >= 1. As soon as I find one person then I'm done.
好吧,只是基于运算符的作用:
Select
将项目放入IQueryable
中,并且在您访问集合之前,它本身在技术上不会执行任何操作;但是,速度将取决于正在查询的集合。Count
枚举整个集合,获取集合中的项目数量,因此对于大型集合,这会“慢”。如果集合包含任何项目并且只需要对第一项进行一次检查即可返回 true,则
Any
返回 true 或 false。Well just based off of what the operators do:
Select
puts items into anIQueryable<T>
and itself technically doesn't do anything until you access the collection; however, speed will depend on the collection being queried.Count
enumerates the entire collection getting the amount of items in the collection, so for large collections this will be 'slow'.Any
returns true or false if the collection contains any items and requires only one check of the first item to return true.我将使用发票作为项目列表或集合。所以你会更好地看到差异。
invoices.Count()
返回一个 int 值,其中包含列表中的项目(发票)总数。invoices.Any()
返回一个布尔值。 True: 如果列表中至少有一项(发票),False: 如果列表中没有一项(发票)。invoices.Select(x => x.Number)
仅返回“Number”列表,不再有任何元素。通过上面的示例,您可以看到以下各项之间的区别:Count、Any 和 Select。
如果您想了解有关这些功能的更多信息,可以阅读以下参考资料:
Count()
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.count?view=net-5.0Any()
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any?view=net-5.0Select()
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=net-5.0I am going to use invoices as a list or collections of items. So you will see the differences much better.
invoices.Count()
Returns an int with the total of items (invoices) are in the list.invoices.Any()
Returns a bool. True: if there is at least one item (invoice) in the list and False: if no one item (invoice) in the list.invoices.Select(x => x.Number)
Return only a list of "Number" with no more elements.With the exmples above you can see the differences among: Count, Any and Select.
If you would like to know more about these functionality you can read the following references:
Count()
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.count?view=net-5.0Any()
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any?view=net-5.0Select()
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=net-5.0