DistinctBy z dwiema właściwościami w VB.NET
Patrzeć naWybierz odrębne według dwóch właściwości na liście możliwe jest użycie metody rozszerzenia DistinctBy z dwiema właściwościami. Próbowałem przekonwertować to na vb.net, ale nie otrzymuję oczekiwanych rezultatów
Klasa testowa:
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
Metoda badania:
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
Czy jest to w ogóle możliwe w vb.net przy użyciu anonimowych typów? Robiąc coś takiego:
Result = TestList.DistinctBy(Function(element) element.Id & "-" & element.Name)
działa, dlatego zgaduję, że brakuje mi czegoś z równością w typach anonimowych.