ReSharper 2026.1 Help

代码检查:替换为 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日