Using VB.net
I have two List(Of String)
Here's how Im finding the common items between the two lists:
Sub Main()
Dim lstOne As New List(Of String)() _
From {"Jim", "Jack", "Kate", "Nope"}
Dim lstTwo As New List(Of String)() _
From {"Jack", "Nope", "Jim"}
Dim lstNew As IEnumerable(Of String) = Nothing
lstNew = lstOne.Intersect(lstTwo, StringComparer.OrdinalIgnoreCase)
End Sub
I want to use Linq to find the uncommon items in these two lists.
How can I do that?
To stick within pure LINQ, you can use Except
:
Dim inOneNotTwo As IEnumerable(Of String) = lstOne.Except(lstNew)
Dim inTwoNotOne As IEnumerable(Of String) = lstTwo.Except(lstNew)
Alternatively, you could use HashSet(Of T)
and SymmetricExceptWith
:
Dim strings As HashSet(Of String) = new HashSet(Of String)(lstOne)
strings.SymmetricExceptWith(lstTwo)
See more on this question at Stackoverflow