【发布时间】:2021-06-24 15:54:47
【问题描述】:
当我尝试使用 LINQ 中的 Sort 方法时出现编译时错误消息。我的代码有什么问题?
提前致谢
public class tt
{
public string zz;
public string yy;
}
...
var tts = new List<tt>();
var sorted = tts.Sort(x => x.yy); // <- Compile Time Error here
编译时错误:
无法将 lambda 表达式转换为类型“IComparer”,因为它不是委托类型
【问题讨论】:
-
.Sort(....)是来自List<T>的方法,而不是 LINQ 方法。 -
您的意思是使用
OrderBy()而不是Sort()?