DistinctBy с двумя свойствами в VB.NET
Смотря наВыделить по двум свойствам в списке можно использовать метод расширения DistinctBy с двумя свойствами. Я пытался преобразовать это в vb.net, но я не получаю ожидаемых результатов
Тестовый класс:
Public Class Test
Public Property Id As Integer
Public Property Name As String
Public Overrides Function ToString() As String
Return Id & " - " & Name
End Function
End Class
Метод испытания:
Private Sub RunTest()
Dim TestList As New List(Of Test)
TestList.Add(New Test() With {.Id = 1, .Name = "A"})
TestList.Add(New Test() With {.Id = 2, .Name = "A"})
TestList.Add(New Test() With {.Id = 3, .Name = "A"})
TestList.Add(New Test() With {.Id = 1, .Name = "A"})
TestList.Add(New Test() With {.Id = 1, .Name = "B"})
TestList.Add(New Test() With {.Id = 1, .Name = "A"})
Dim Result As IEnumerable(Of Test)
Result = TestList.DistinctBy(Function(element) element.Id)
'1 - A
'2 - A
'3 - A
Result = TestList.DistinctBy(Function(element) element.Name)
'1 - A
'1 - B
Result = TestList.DistinctBy(Function(element) New With {element.Id, element.Name})
'1 - A
'2 - A
'3 - A
'1 - A
'1 - B
'1 - A
'Expected:
'1 - A
'2 - A
'3 - A
'1 - B
End Sub
Это вообще возможно в vb.net, используя анонимные типы? Делать что-то вроде этого:
Result = TestList.DistinctBy(Function(element) element.Id & "-" & element.Name)
работает, поэтому я предполагаю, что я что-то упустил с равенством в анонимных типах здесь.