morelinq - DistinctBy with two properties in VB.NET -
looking @ select distinct 2 properties in list possible use distinctby extensionmethod 2 properties. tried convert vb.net, i'm not getting expected results
test class:
public class test public property id integer public property name string public overrides function tostring() string return id & " - " & name end function end class
test method:
private sub runtest() dim testlist new list(of test) testlist.add(new test() {.id = 1, .name = "a"}) testlist.add(new test() {.id = 2, .name = "a"}) testlist.add(new test() {.id = 3, .name = "a"}) testlist.add(new test() {.id = 1, .name = "a"}) testlist.add(new test() {.id = 1, .name = "b"}) testlist.add(new test() {.id = 1, .name = "a"}) dim result ienumerable(of test) result = testlist.distinctby(function(element) element.id) '1 - '2 - '3 - result = testlist.distinctby(function(element) element.name) '1 - '1 - b result = testlist.distinctby(function(element) new {element.id, element.name}) '1 - '2 - '3 - '1 - '1 - b '1 - 'expected: '1 - '2 - '3 - '1 - b end sub
is @ possible in vb.net using anonymous types? doing this:
result = testlist.distinctby(function(element) element.id & "-" & element.name)
is working, therefore i'm guessing i'm missing equality in anonymous types here.
you need write key
before property. like
new {key element.id, key element.name}
in vb.
so,
result = testlist.distinctby(function(element) new {key element.id, key element.name})
see documentation anonymous types in vb more details.
Comments
Post a Comment