Remove duplicates froms a list
Emmanuel Ruellan
Hi all,I'm having trouble removing duplicates from a list.
As a test, I added the following code to a function button and I'm getting an error message.
[CODE]Try
' Create a list of integers.
Dim ages As New System.Collections.Generic.List(Of Integer)(New Integer() _
{21, 46, 46, 55, 17, 21, 55, 55})
' Select the unique numbers in the List.
' THIS IS THE PART THAT ISN'T WORKING
Dim distinctAges As System.Collections.Generic.IEnumerable(Of Integer) = ages.Distinct()
Dim output As New System.Text.StringBuilder("Distinct ages:" & vbCrLf)
For Each age As Integer In distinctAges
output.AppendLine(age)
Next
' Display the output.
MsgBox(output.ToString)
' This code should produce the following output:
'
' Distinct ages:
' 21
' 46
' 55
' 17
Catch ex As Exception
customize.Messaging.StatusBar.WriteError("Erreur : lister sources : " + ex.Message)
End Try
[/CODE]
The error message reads "'Distinct' is not a member of 'System.Collections.Generic.List(Of Integer)'. (BC30456)"
The code is slightly adapted from sample code provided by Microsoft ( [url="http://msdn.microsoft.com/en-us/library/bb348436.aspx#Y0"]http://msdn.microsof...b348436.aspx#Y0[/url] ).
How can I get rid of this error message?
Best regards,
Emmanuel Ruellan
Emmanuel Ruellan
It works if I use a dictionary to remove duplicates.Specifically, I replaced...
[CODE]
Dim distinctAges As System.Collections.Generic.IEnumerable(Of Integer) = ages.Distinct()
[/CODE]
... with :
[CODE] Dim ageDict As New System.Collections.Generic.Dictionary(Of Integer, Integer)
For Each age In ages
ageDict(age) = 1
Next[/CODE]
...and then I looped over ageDict.Keys().
Is it because of the version of VB.Net used by Coresuite Customize?
0
Please sign in to leave a comment.
Comments
0 comments