代码检查:替换为 OfType<T>().First()(替换为 OfType<T>().First(..))
此检查报告了一个使用 as 对元素进行类型转换、使用 null 进行过滤,然后查找满足额外条件的第一个元素的 LINQ 查询。 相同的查询使用 OfType<T>().First(...) 更易阅读。
示例
var first = items.Select(x => x as Person).First(y => y != null && y.IsActive);
var first = items.OfType<Person>().First(y => y.IsActive);
快速修复
将 Select(... as T).First(y => y != null && ...) 模式替换为 OfType<T>().First(...)。
2026年 5月 8日