Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using Left Outer Join in LINQ

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 419
    Comment on it

    We use left outer join when we want each element of the first collection should be fetched irrespective of whether it has the related element in the second collection  or not. In LINQ, we perform left outer join by calling DefaultIfEmpty() method on the result of a group join.

     

    Here is the demonstration of how we can use the DefaultIfEmpty() method.

     

    Suppose we have two classes Person and PersonCars. The class definitions are as follow.

     

       class Person
            {
                public string PersonId { get; set; }
                public string FirstName { get; set; }
                public string LastName { get; set; }
            }
    
       class PersonCars
            {
                public string PersonCarId { get; set; }
                public string PersonId { get; set; }
                public string CarName { get; set; }
            }
    

    Now we want to add entries to the collections of these classes.

    Person firstPerson = new Person { PersonId = "62d8e47a-a915-4614-bded-ea5028b4690d", FirstName = "Abhishek", LastName = "Jaiswal" };
    
    Person secondPerson = new Person { PersonId = "272a5ada-a16b-490f-93d3-9cc730c35485", FirstName = "Shubham", LastName = "Mamgain" };
    
    List<Person> persons = new List<Person> { firstPerson, secondPerson };
    
    PersonCars firstPersonCar = new PersonCars{PersonCarId = "6dfb88b8-05f6-4292-8129-1486cc89e117", PersonId = "62d8e47a-a915-4614-bded-ea5028b4690d", CarName = "Ciaz" };
    
    List<PersonCars> personCars = new List<PersonCars> { firstPersonCar };
    

     

    Now if I want to perform the left outer join so that I could get all the records from persons and get the related value from personcars and if personcars does not contain the related value then “No Car” as the car name then I will write something like this

    var result = from person in persons
                            join personCar in personCars on person.PersonId equals personCar.PersonId into car
                            from x in car.DefaultIfEmpty()
                            select new { person.FirstName, CarName = (x == null ? "No Car" : x.CarName) };
    

    This will give me the output like this.

     

    Person Name : Abhishek, Car Name : Ciaz

    Person Name : Shubham, Car Name : No Car

    So we are getting all the records from Persons but getting the car name only if the related value is existing in personCars and if the related value does not exists then we are printing no car.

     

    It was a brief demonstration of LeftOuterJoin. I hope this will help you implement the same in your code.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: