【发布时间】:2020-11-15 21:15:45
【问题描述】:
所以我尝试使用 lambda 对我的对象列表进行排序,如下所示:
List<DirectoryObjects> sort = dirObjList.OrderBy(ms => ms.name);
Visual Studio 虽然说这是错误的,但它说这是正确的:
List<DirectoryObjects> sort = (List<DirectoryObjects>)dirObjList.OrderBy(ms => ms.name);
我以前接触过 lambda,但我从来没有真正理解过它。我的做法对我来说似乎是正确的,因为我的印象是 lambda 只是为我排序我的列表,但仍然保留它的类型。这样想有错吗?第二个实例看起来……对我来说是多余的。当它已经是这样时,为什么我需要转换一个排序列表?还是我应该将 lambdas 视为将 Enum 转换为不同类型的东西?
【问题讨论】:
-
这能回答你的问题吗? OrderBy and List vs. IOrderedEnumerable
-
dirObjList.OrderBy(ms => ms.name).ToList()这几乎应该作为错字关闭。也许你应该研究一下 IEnumerable 及其派生类和 List 的区别 -
Visual Studio though says that is wrong and it says this is correct:要清楚,Visual Studio 从来没有说它是正确的。它可能会说它编译 - 但它肯定不是正确(对于correct的任何有意义的定义)。