Code inspection: Replace with OfType<T>().Count()
This inspection reports a LINQ query that casts elements with as and then counts non-null results. The same count can be expressed more directly with OfType<T>().Count().
Example
int count = items.Select(x => x as string).Count(y => y != null);
int count = items.OfType<string>().Count();
Quick-fix
Replace the Select(... as T).Count(y => y != null) pattern with OfType<T>().Count().
29 March 2026