vb.net 使用 VB 和 LINQ 对 <Object> 列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9520221/
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
Sorting a list of <Object> with VB and LINQ
提问by WhiskerBiscuit
I'm trying out some LINQ expressions and can't get them to work with the List class. Basically I want to be able to sort a list of custom objects by property type, however the C# LINQ syntax is KILLING me and I can't figure out how to convert it to VB
我正在尝试一些 LINQ 表达式,但无法让它们与 List 类一起使用。基本上我希望能够按属性类型对自定义对象列表进行排序,但是 C# LINQ 语法正在杀死我,我无法弄清楚如何将其转换为 VB
Class Foo
Sub New(Name As String, Position As Integer)
Me.Name = Name
Me.Position = Position
End Sub
Public Name As String
Public Position As Integer
End Class
Sub Main()
Dim l As New List(Of Foo)
l.Add(New Foo("C", 3))
l.Add(New Foo("B", 2))
l.Add(New Foo("A", 1))
Dim asc = ..... (sort l by position asecnding)
Dim desc = ..... (sort l by position descending)
End Sub
回答by harriyott
Dim asc = From f In l Order By f.Position
Dim desc = From f In l Order By f.Position Descending
回答by AnarchistGeek
I used c# to VB converter..
我用 c# 到 VB 转换器..
Dim sortedasc = l.OrderBy(Function(k) k.Position)
Dim sorteddesc = l.OrderByDescending(Function(k) k.Position)
this should work..
这应该工作..