开发者

are these two linq expressions functionally equivalent?

开发者 https://www.devze.com 2023-01-26 20:20 出处:网络
is there any functional difference between: if (photos.Any(it => it.Archived)) and if (photos.Where(it => it.Archived).Any())

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开发者_StackOverflow社区?


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.


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消