Results 1 to 3 of 3

Thread: Having trouble using Linq to return unique values when comparing two lists

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Having trouble using Linq to return unique values when comparing two lists

    Hello,

    I have two lists,
    List 1, TeamsList contains 18 unique values (the complete list)
    List 2, sortplayinglist can contain anywhere from 2 to 18 unique values (a subset of the complete list)

    The values, while unique for each list, are the same. I am simply wanting to get a list of what isn't in list 2 when compared to list one which is the complete list

    I am trying to create a third list called notplayingteamslist that only contains the values from the TeamsList that do not appear in the sortplayinglist

    Code:
    Dim notplayingteamslist = TeamsList.except(sortplayinglist)
    If notplayingteamslist.count > Nothing Then
        For Each team As String In notplayingteamslist
              Debug.Print(team)
        Next
    End If
    After working through the microsoft references for Linq, I boiled it down to the code above,
    but unfortunately nothing gets populated into notplayingteamslist.

    Any hints are greatly appreciated.

    Regards,
    Antony

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Having trouble using Linq to return unique values when comparing two lists

    Code:
    Dim notplayingteamslist As ??? = TeamsList.except(sortplayinglist)
    Code:
    If notplayingteamslist.count > Nothing Then ‘ ???
    Code:
    If notplayingteamslist.count > 0 Then
    If The 3 Lists are of type List(Of String), be aware that inconsistent naming or character casing won’t work.
    This is how LINQ Except works…

    Code:
    Dim allList As New List(Of String)(New String() {“One”, “Two”, “Three”, “Four”, “Five”})
    Dim oddList As New List(Of String)(New String() {“One”, “Three”, “Five”}) 
    Dim evenList As List(Of String) =allList.Except(oddList).ToList

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2026
    Posts
    33

    Re: Having trouble using Linq to return unique values when comparing two lists

    Thanks Paul,

    Your tips put me on the right track

    Here is the code that works

    Code:
    Dim teamslist As IEnumerable(Of String) = Global.TeamsList
    Dim notplayingteamslist As IEnumerable(Of String) = teamslist.Except(playinglist)
    If notplayingteamslist.Count > Nothing Then
        For Each team As String In notplayingteamslist
            Debug.Print(team)
        Next
    End If
    I had to clone the global variable I was using and make it a local string array.
    Not really understanding why the global variable wouldn't work with except.
    The conditional is only to catch a null value so that I don't get an out of range error.

    Cheers.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width