Click here to Skip to main content
15,885,878 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Conveniently Remove a List of Items From Another List

Rate me:
Please Sign up or sign in to vote.
4.93/5 (7 votes)
14 Aug 2012CPOL 27K   7   1
Convenient way to remove items from list when items exist in second list.
Say you have two lists. You want to remove the items from the first list that already exist in a second list:
VB.NET
Dim listFirst As List(Of String) = New List(Of String)
Dim listSecond As List(Of String) = New List(Of String)

listFirst.Add("A")
listFirst.Add("B")
listFirst.Add("C")
listFirst.Add("D")
listFirst.Add("E")
listFirst.Add("F")

listSecond.Add("A")
listSecond.Add("D")
listSecond.Add("F")

Dim different As IEnumerable(Of String) = listFirst.Except(listSecond)
listFirst = different.ToList()
Result:
B
C
E 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionExcellent! Pin
Member 1034462819-Dec-22 20:02
Member 1034462819-Dec-22 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.