不能使用 linq “Last()” 方法

本文关键字:方法 Last linq 不能 | 更新日期: 2023-09-27 18:33:28

我对C#了解不多,但我正在做这个项目,我想做这样的事情:

SortedDictionary<int, List<ChessMove>> possibleMovesByRank = new SortedDictionary<int, List<ChessMove>>();
...
var best = possibleMovesByRank.Keys.Last();

从我能够找到的内容来看,这应该使用 linq 返回具有最高值的键,但是 VS 给了我一个错误:

SortedDictionary.KeyCollection does not contain a definition for 'Last' and no extension method for 'Last'

我是否遗漏了某些内容,或者我的项目设置不正确?

不能使用 linq “Last()” 方法

最有可能的是,您的 C# 文件中缺少 System.Linq 命名空间。这是一个扩展方法,除非包含相关的命名空间,否则 Enumerable.Last 将不存在。