vb.net Lambda OrderBy 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13202068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:23:53  来源:igfitidea点击:

Lambda OrderBy method

vb.netlinqlambda

提问by Princess

I have a graphing class to detect circular dependencies in some business logic I am writing. My graphing class builds nodes that knows the relationship to other nodes.

我有一个图形类来检测我正在编写的某些业务逻辑中的循环依赖关系。我的绘图类构建了知道与其他节点的关系的节点。

I have nodeList as List(of Objects) each having a List(of String)

我有 nodeList 作为 List(of Objects) 每个都有一个 List(of String)

I was thinking that the below line of code would yield the correct sorting. I thought wrong.

我在想下面的代码行会产生正确的排序。我想错了。

nodeList.OrderByDescending(Function(x) x.Count)

I want to reorder my nodeList in descending order by the List(of String).Count.

我想通过 List(of String).Count 按降序重新排序我的 nodeList。

my List(of Object)
(0) | Count = 3
(1) | Count = 5
(2) | Count = 2

My desired output List(of Object)
(0) | Count = 5
(1) | Count = 3
(2) | Count = 2

回答by D Stanley

OrderByDescendingdoes not reorder the list in-place. It returnsan enumerator that you can use to get a new ordered list. You need to use .ToList()to replace the original list:

OrderByDescending不会就地重新排序列表。它返回一个枚举器,您可以使用它来获取新的有序列表。您需要使用.ToList()来替换原始列表:

 nodeList = nodeList.OrderByDescending(Function(x) x.Count).ToList()