Today I came across a LINQ method, DefaultIfEmpty(), which is quite similar to the Left Join of SQL.

DefaultIfEmpty works like a left join and gives all the records from the left table including the matching records from the right table. Use DefaultIfEmpty<TSource>(IEnumerable<TSource>) to provide a default value in case the source sequence is empty.

For more information about DefaultIfEmpty(), please have a look into this link: http://msdn.microsoft.com/en-us/library/bb397895.aspx

Kindly look into the code given below:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace LINQ_IfByDefault

{

 

    //Class Employee having two properties

    class Employee

    {

        public string Name { get; set; }

        public string EmpID { get; set; }

 

    }

    //Class Worker having two properties

    class Worker

    {

        public string WId { get; set; }

        public string City { get; set; }

    }

   

   

    class Program

    {

        static void Main(string[] args)

        {

            //Object Initialization for Employee class

              List<Employee> objEmployee = new List<Employee>{

                new Employee{ Name="Sachin",EmpID="I001"},

                new Employee{ Name="Vijay",EmpID="I002"},

                new Employee{ Name="Ashish",EmpID="I003"},

                new Employee{ Name="Syed",EmpID="I004"},

                new Employee{ Name="Ravish",EmpID="I005"},

            };

              //Object Initialization for Worker class

            List<Worker> objWorker = new List<Worker>{

                new Worker{ WId="I001",City="Delhi"},

                new Worker{ WId="I002",City="Haridwar"},

                new Worker{ WId="I007",City="Roorkee"},

                new Worker{ WId="I008",City="Amritsar"},

                new Worker{ WId="I009",City=""},

            };

 

 

            //using of DefaultIfEmpty method provided by LINQ

            var resultDefaultIfEmpty = from emp in objEmployee

                                       join worker in objWorker on emp.EmpID                  equals worker.WId into ResultEmpWorker

                                      

from output in ResultEmpWorker.DefaultIfEmpty()

select new

  {

     EmployeeName = emp.Name,    

     City = output!= null ? output.City : null

  };

                      Console.WriteLine(string.Join("n", resultDefaultIfEmpty.Select(emp => " Employee Name = " +

                      emp.EmployeeName + ", City Name = " + emp.City).ToArray<string>()));

                       Console.ReadLine();

 

 

        }

       

    }

  

}

I have also observed a few things which may be a bit beneficial for you; whilst working with defaultifempty, the line in the above code segment:


Linq1.jpg

The output variable contains only records from the right side list of the Join Clause; in our case it is Worker List:

Linq2.jpg

Please have a look into how DefaultIfEmpty() works.

Your thoughts are highly appreciated.

Keep Coding.

 

Next Recommended Readings