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
Please sign in to leave a comment.
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.
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.