我正在尝试一些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
答案 0 :(得分:13)
Dim asc = From f In l Order By f.Position
Dim desc = From f In l Order By f.Position Descending
答案 1 :(得分:10)
我用c#转换VB转换器..
Dim sortedasc = l.OrderBy(Function(k) k.Position)
Dim sorteddesc = l.OrderByDescending(Function(k) k.Position)
这应该有用..