Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are these two linq expressions functionally equivalent?

Tags:

c#

linq

is there any functional difference between:

if (photos.Any(it => it.Archived))

and

if (photos.Where(it => it.Archived).Any())

if so, is there a more compelling reason to use one over the other?

like image 867
CedricB Avatar asked Nov 23 '10 21:11

CedricB


3 Answers

Functionally they do the same thing. The performance depends on the specific provider, but for example in LINQ to SQL both forms produce the exact same SQL:

SELECT 
    (CASE 
        WHEN EXISTS(
            SELECT NULL AS [EMPTY]
            FROM [dbo].[photos] AS [t0]
            WHERE [t0].[Archived] = 1
            ) THEN 1
        ELSE 0
     END) AS [value]

If I have to pick one I'd use the first option as it seems slightly more clear to me.

like image 83
Mark Byers Avatar answered Oct 22 '22 01:10

Mark Byers


As an expression, they produce the same result. However, the mechanism that produces this result is slightly different. Note though that they both walk through the same number of items from the original sequence.

I would always use the former as it is shorter and there is no loss of clarity, readability nor maintainability in choosing it over the second.

like image 2
jason Avatar answered Oct 22 '22 01:10

jason


No, there is no difference practical difference for the current .NET Framework implementation of the methods. Because of deferred execution, both will enumerate photos until it finds a match, and then stop. (Provided that this is Linq-to-objects - other providers might behave different).

If you want to get nit-picky, there are small implementational differences. (You can look it up in the framework sources):

Any simply iterates the collection using foreach, where as Where returns either a WhereArrayIterator, WhereListIterator, or a WhereEnumerableIterator depending on what type of collection you pass in. I haven't looked into this in depth, but I would guess that this is done in order to allow chained extension methods to optimize in case of an array or list is used.

In other words, where Any contains a simple foreach, Where does this:

    if (source is TSource[]) return new WhereArrayIterator<TSource>((TSource[])source, predicate); 
    if (source is List<TSource>) return new WhereListIterator<TSource>((List<TSource>)source, predicate);
    return new WhereEnumerableIterator<TSource>(source, predicate);
like image 2
driis Avatar answered Oct 22 '22 01:10

driis