none
How to filter objects in two List ( Of ... ) using Linq and Join ? RRS feed

  • Question

  • Hi everybody.

    I'm quite new to Linq.

    I've got two Classes :

    1. Class clsPerson ( Properties : ID As Integer, Name As String, Office As clsOffice )

    2. Class clsOffice ( Properties : ID As Integer, Description As String )

    I've made two test Lists with some test data :

    A List of Persons and a List of Offices.

    Now I'd Like to filter only the Persons in the List with the correct Office Property ( which has "Office 1" in Description ),

    and trying to do so with a Linq Join :

            Dim L As List(Of clsPerson) = (From P In objListOfPersons
                                           Join O In objListOfOffices On P.Office Equals O
                                           Select P
                                           Where O.Description = "Office 1").ToList

    But VS complains that the "O" object in the Where clause is not declared or out of scope.

    Where should I declare it then ?

    Thanks for help.

    Wednesday, February 22, 2017 11:33 AM

Answers

  • See the following example showing how to do your join. There are various versions to work with.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by MarcoGG Wednesday, February 22, 2017 1:23 PM
    Wednesday, February 22, 2017 11:56 AM
    Moderator

All replies

  • See the following example showing how to do your join. There are various versions to work with.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by MarcoGG Wednesday, February 22, 2017 1:23 PM
    Wednesday, February 22, 2017 11:56 AM
    Moderator
  • See the following example showing how to do your join. There are various versions to work with.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thank You.

    That was so simple, but I couldn't imagine the Where clause had to be put before the Select one ! :D

    So my example now is :

     Dim L As List(Of clsPerson) = (From P In objListOfPersons
                                    Join O In objListOfOffices On P.Office Equals O
    				Where O.Description = "Office 1"
                                    Select P).ToList
    and works as expected.

    Wednesday, February 22, 2017 1:22 PM