.Where()
call CS-R1046.Where(predicate).Count()
as .Count(predicate)
45
46 // CS-R1046: Rewrite `arr.Where(x => x % 3 == 0).Count()` as `arr.Count(x => x % 3 == 0)`.
47 // https://deepsource.io/directory/analyzers/csharp/issues/CS-R1046
48 var threeMultiples = arr.Where(x => x % 3 == 0).Count(); 49
50 // CS-R1047: Use `T?` instead of `Nullable<T>`.
51 // https://deepsource.io/directory/analyzers/csharp/issues/CS-R1047
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.
var count = arr.Where(x => x % 5 == 0).Count();
var count = arr.Count(x => x % 2 == 0);