代码检查:替换为 OfType<T>().Where()(替换为 OfType<T>().Where(..))
此检查报告了使用 as 对元素进行类型转换,使用 null 进行过滤,然后应用另一个 Where(...) 谓词的 LINQ 查询。 使用 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日