代码检查:替换为 OfType<T>().Where()(替换为 OfType<T>().Where(..))
此检查会报告 LINQ 查询使用 as 转换元素,使用筛选器筛选 null ,然后再应用另一个 Where(...) 谓词。 使用 OfType<T>().Where(...) 可以让同样的查询更清晰。
示例
var query = items.Select(x => x as Person).Where(y => y != null && y.IsActive);
var query = items.OfType<Person>().Where(y => y.IsActive);
快速修复
将 Select(... as T).Where(y => y != null && ...) 模式替换为 OfType<T>().Where(...)。
2026年 5月 8日