Why the suggestion "Replace with single call too..."

I've written a LINQ query against some in-memory objects.  Given this statement...

detail.Locations.Where(x => x.ACFunds == 101m).Single().FederalFunds


...ReSharper 8.2 suggests that I change it to...

detail.Locations.Single(x => x.ACFunds == 101m).FederalFunds

Why does ReSharper suggest that?  

Thanks
Marty

2 comments
Comment actions Permalink

Simply because it's more efficient. Rather than create an enumerator for both the Where clause and the Single operator, the alternative only creates a single enumerator.

0
Comment actions Permalink

But Single throws an exception if more than 1 exists or if it doesn't find a matching element, so not doing the same job and has no default value.

0

Please sign in to leave a comment.