【发布时间】:2019-02-08 21:10:25
【问题描述】:
我有一个列表(自定义类)
我想通过 linq 从中提取所有“名称”字符串的列表
我知道如何处理循环,但我需要使用线性、简短的 linq 指令来获得它。
我已经检查了这个帮助 C# Extract list of fields from list of class
但是我在 linq 正确的语法上有问题
特别是因为我想提取一个新列表(字符串)
Class Student
Sub New(ByVal NewName As String, ByVal NewAge As Integer)
Name = NewName
Age = NewAge
End Sub
Public Name As String
Public Age As Integer
End Class
Public Sub Main
Dim ClassRoom as New List(Of Student) From {New Student("Foo",33), New Student("Foo2",33), New Student("Foo3",22)}
Dim OneStudent as Student = ClassRoom(0)
Dim AllStudentsNames As New List(Of String) From {ClassRoom.Select(Function(x) x.Name <> OneStudent.Name).ToList}
End Sub
但是有些不对劲……
有什么帮助吗?
附:由于 c# 接近 vb.Net,因此也欢迎 c# 帮助。
【问题讨论】: