代码检查:替换为 OfType<T>().First()
此检查会报告使用 as 对元素进行强制转换,然后用 First(...) 查找第一个非空转换结果的 LINQ 查询。 使用 OfType<T>().First() 可以更清晰地表达相同意图。
示例
var first = items.Select(x => x as string).First(y => y != null);
var first = items.OfType<string>().First();
快速修复
将 Select(... as T).First(y => y != null) 模式替换为 OfType<T>().First()。
2026年 5月 8日