代码检查:替换为 OfType<T>().Count()(替换为 OfType<T>().Count(..))
此检查报告了一个 LINQ 查询,该查询使用 as 对元素进行转换,筛选器 null ,然后统计符合其他条件的元素数量。 使用 OfType<T>().Count(...) 可以让逻辑更清晰。
示例
int count = items.Select(x => x as Person).Count(y => y != null && y.IsActive);
int count = items.OfType<Person>().Count(y => y.IsActive);
快速修复
将 Select(... as T).Count(y => y != null && ...) 模式替换为 OfType<T>().Count(...)。
2026年 5月 8日