Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This refactoring applies to:
- C#
What: Refactors instances of SomeEnumerableType.Where(<LambdaExpression>).Single() to SomeEnumerable.Single(<LambdaExpression>) for Enumerable.Single() as well as the following Enumerable methods: SingleOrDefault(), Last(), LastOrDefault(), Any(), Count(), First(), and FirstOrDefault().
When: All instances where the method calls Single(), SingleOrDefault(), and so on, doesn't have any arguments and is preceded by a Where() expression. The input to the Where() expression cannot be constructed as an expression tree.
Why: Removing the unnecessary call to the Enumerable for the .Where() method improves readability and in some cases performance, see remarks.
How-to
Place your cursor within the
SomeEnumerableType.Where(<LambdaExpression>).Single()instance in visual studio.Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
Select Simplify LINQ expression

Remarks
In some cases this refactoring may reduce performance. LINQ operations on List<T> and T[] are not optimized in this case and result in worse performance.