If you have a list, array, or query in a C#/LINQ application and need to check and see if the list is empty, the correct way to do this is to use the Any() extension method:
if (q.Any()) {
Similarly, you can check to see if any elements in the list meet a certain condition:
if (q.Any(i => i.IsSpecial)) {
If the query provider is something like LINQ to Entities, this will be translated into fairly efficient SQL using EXISTS.
For some reason, I see a lot of people write this code using the Count() extension method instead (maybe they don’t know about Any()?), like this:
if (q.Count() > 0) {
This is wrong for two reasons:
- It’s imperative instead of expressive. Using
Any()tells the query provider, "Please determine if the list is non-empty using the most efficient way you can." UsingCount() > 0tells the query provider, "Please do exactly what I tell you, regardless of what I really need." Because LINQ is intended to support a variety of query providers, it is important to be expressive instead of imperative, and to let the provider specialize the implementation. - Because we don’t actually care about the exact count of items in the list, only that it is greater than zero, using the
Count()function can cause the provider to do considerably more work than necessary. Consider a linked list, or a SQL-based provider.
{ 6 } Comments
Not using Count() if you don’t actual need the exact number, is very important as you lined out - agreed.
"It’s imperative instead of expressive." is itself not a reason against using Count() imho, as this is very simplistic example only. There are real world situations, where you have to count or - more abstract - where you have to compare two values - even in expressive LINQs
Agreed - this is a great post for anyone who is concerned about the performance of generated SQL using LINQ.
Can we use ‘Any’ for null objects?
Thanks
Ray Akkanson
Not sure what you mean, Ray, but in general you can use
.Any()anywhere where you can use.Count().Thanks. This is very useful information.
Ray Akkanson
Compare sql generated by EF 5.0.
Count()
———————————————————————————————-
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Members] AS [Extent1]
WHERE [Extent1].[Uguid] = @p__linq__0
———————————————————————————————-
Any()
———————————————————————————————-
SELECT
CASE WHEN ( EXISTS (SELECT 1 AS [C1] FROM [dbo].[Members] AS [Extent1] WHERE [Extent1].[Uguid] = @p__linq__0)) THEN cast(1 as bit)
WHEN ( NOT EXISTS (SELECT 1 AS [C1] FROM [dbo].[Members] AS [Extent2] WHERE [Extent2].[Uguid] = @p__linq__0)) THEN cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X )
———————————————————————————————-
{ 1 } Trackback
[...] استفاده از Any به جای Count وقتی منظورتون همون Any است. [...]
Post a Comment