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