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