none
Help with a Linq Expression RRS feed

  • Question

  • I have an Organizstions = ObservableCollection(of Organization) where Organization is a class with OrganizationID as the primary key plus other fields.  I also have a List(of Vendor) which has just one field, OrganizationID.  I hope it's obvious that the List just has the ID of Organizations that are also vendors.

    I want to write a Linq expression to get an ienumerable of all the organizations that are NOT vendors.  I can get this far:
    Dim NonVendors = From o in Organizations
                                 Join v In Vendors
    			     On o.ID NotEqual v.ID
    But that doesn't work because NotEqual is not a valid operator (It should be).  How can "NotEqual" be written?

    • Edited by SezMe Saturday, June 29, 2019 11:26 PM
    Saturday, June 29, 2019 11:26 PM

Answers

  • Try this too:

    Dim NonVendors = From o In Organisations
                     Where Not (From v In Vendors Select v.OrganisationID).Contains(o.OrganisationID)
                     Select o


    • Edited by Viorel_MVP Sunday, June 30, 2019 5:06 PM
    • Marked as answer by SezMe Sunday, June 30, 2019 10:31 PM
    Sunday, June 30, 2019 5:06 PM

All replies

  • What you want is a left join rather than an outer join 

    https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, June 30, 2019 1:46 AM
    Moderator
  • Left join c# converted to VB

    Friend Class SurroundingClass
        Friend Class Person
            Public Property FirstName As String
            Public Property LastName As String
        End Class
    
        Friend Class Pet
            Public Property Name As String
            Public Property Owner As Person
        End Class
    
        Public Shared Sub LeftOuterJoinExample()
            Dim magnus As Person = New Person With {
                .FirstName = "Magnus",
                .LastName = "Hedlund"
            }
            Dim terry As Person = New Person With {
                .FirstName = "Terry",
                .LastName = "Adams"
            }
            Dim charlotte As Person = New Person With {
                .FirstName = "Charlotte",
                .LastName = "Weiss"
            }
            Dim arlene As Person = New Person With {
                .FirstName = "Arlene",
                .LastName = "Huff"
            }
            Dim barley As Pet = New Pet With {
                .Name = "Barley",
                .Owner = terry
            }
            Dim boots As Pet = New Pet With {
                .Name = "Boots",
                .Owner = terry
            }
            Dim whiskers As Pet = New Pet With {
                .Name = "Whiskers",
                .Owner = charlotte
            }
            Dim bluemoon As Pet = New Pet With {
                .Name = "Blue Moon",
                .Owner = terry
            }
            Dim daisy As Pet = New Pet With {
                .Name = "Daisy",
                .Owner = magnus
            }
            Dim people As List(Of Person) = New List(Of Person) From {
                magnus,
                terry,
                charlotte,
                arlene
            }
            Dim pets As List(Of Pet) = New List(Of Pet) From {
                barley,
                boots,
                whiskers,
                bluemoon,
                daisy
            }
            Dim query = From person In people Group Join pet In pets On person Equals pet.Owner Into gj = Group From subpet In gj.DefaultIfEmpty() Select New With {person.FirstName,
                .PetName = If(subpet?.Name, String.Empty)
            }
    
            For Each v In query
                Console.WriteLine($"{v.FirstName & ":",-15}{v.PetName}")
            Next
        End Sub
    End Class

    Sunday, June 30, 2019 4:37 AM
  • Try this too:

    Dim NonVendors = From o In Organisations
                     Where Not (From v In Vendors Select v.OrganisationID).Contains(o.OrganisationID)
                     Select o


    • Edited by Viorel_MVP Sunday, June 30, 2019 5:06 PM
    • Marked as answer by SezMe Sunday, June 30, 2019 10:31 PM
    Sunday, June 30, 2019 5:06 PM
  • @DA924x This is what I got from you solution:

    Magnus:        Daisy
    Terry:         Barley
    Terry:         Boots
    Terry:         Blue Moon
    Charlotte:     Whiskers
    Arlene:       

    The last line containing "Arlene" should not be included in the Linq results.

    Sunday, June 30, 2019 10:25 PM