Tip Of the Day - Remove Duplicate Items from a List(Of T) (or a List of whatever the hell you want)

20. September 2009

Read this article in your language IT | EN | DE | ES

If you need to remove duplicates from a from a collection that implements IEnumerable without iterating through or overriding the .equals method you can do the following:


Dim Cars As New List(Of String)(New String() {"Blue", "Red", "Yellow", "Red", "Black", "Blue"})
Cars = Cars.Distinct().ToList()

Your list can be of anything you want. Strings, objects etc.. you can just for duplicates with the Distinct().ToList() method.

Code Snippets, Tip of the Day, VB.NET , ,

Add comment


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading