代码检查:替换为 OfType<T>().LongCount()
此检查会报告使用 as 强制转换元素,然后使用 LongCount(...) 统计非空结果的 LINQ 查询。 使用 OfType<T>().LongCount() 可以更直接地完成相同的计数。
示例
long count = items.Select(x => x as string).LongCount(y => y != null);
long count = items.OfType<string>().LongCount();
快速修复
将 Select(... as T).LongCount(y => y != null) 模式替换为 OfType<T>().LongCount()。
2026年 5月 8日