C#

C#

By DeepSource

Consider simplifying LINQ query by dropping explicit `.Where()` callCS-R1046

Anti-pattern
Has Autofix

The expression .Where(predicate).Count() returns the number of elements satisfying the predicate. However, this entire expression can be simplified by dropping the .Where() call and directly passing the predicate to .Count(). This is more concise and succinct.

Examples

Bad Practice

var count = arr.Where(x => x % 5 == 0).Count();

Recommended

var count = arr.Count(x => x % 2 == 0);