Dot Net For All

How to use Join in LINQ with C#

In this article I will show how to use Join in Linq with C# examples.  Joins in Linq work much like the join in relational database but there are some subtle difference which you will find below.

You may find the below article informative.

How to Use Join

Joins are basically used to join two data collections to get the desired results. There are basically two sequences of data. One is outer sequence and other is inner sequence.

In Linq we use equal keyword to join two sequence unlike sql where we use ‘=’ operator.

The benefit of using Join instead of sub query is that it creates a key collection of the inner sequence. Suppose you have Employee and Department collections and you want to join both of them. Department being the inner sequence join will create a key collection for Department sequence and pull out the relevant department for each employee from the key collection.

The key collection is hash table and it improved the performance.

These LINQ join is like the inner join of SQL. We will get only the correlated data from both the sequences and removing out the data which is not present in any of the sequence.

If we have an employee whose department is not present in that case we will not get the particular employee in the result.

Lets see a working example of Join in LINQ.

class EmployeeRepository
    {
        public static IEnumerable<Employee> _employee = GetEmployee();

        private static IEnumerable<Employee> GetEmployee()
        {
            return new List<Employee>() { 
                new Employee(){ DepartmentID = 1, EmployeeName = "Vikram Chaudhary", EmployeeID = 1},
                new Employee(){ DepartmentID = 1, EmployeeName = "Charu Singh", EmployeeID = 2},
                new Employee(){ DepartmentID = 2, EmployeeName = "Robert ", EmployeeID = 3},
                 new Employee(){ DepartmentID = 3, EmployeeName = "Albert ", EmployeeID = 4},
            };
        }        
    }

    class DepartmentRepository
    {
        public static IEnumerable<Deparment> _department = GetDepartments();

        private static IEnumerable<Deparment> GetDepartments()
        {
            return new List<Deparment>() { 
                new Deparment(){ ID = 1, Name = "Engineering"},
                new Deparment(){ ID = 2, Name = "Sales"}
            };
        }
    }

    class Employee
    {
        public int EmployeeID { get; set; }
        public string EmployeeName { get; set; }
        public int DepartmentID { get; set; }
    }

    class Deparment
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

     static void Main(string[] args)
        {
            var employeeAndDep = from emplyee in EmployeeRepository._employee
                                 join
                                     dep in DepartmentRepository._department on emplyee.DepartmentID equals dep.ID
                                 select new { Employee = emplyee.EmployeeName, Department = dep.Name };

            foreach (var item in employeeAndDep)
            {
                Console.WriteLine("{0} {1}", item.Employee, item.Department);
            }

            Console.Read();          
        }

In the above code I have two repositories for Employee and Department. In my main method I am using both of these repositories to get the department for each employee.  I hope the code is self explanatory.

Lets see the output of the code below.

How to use Join in LINQ

As we an see in the result “Albert” is missing from the result set as the corresponding department for this employee is not present.

I hope you will like this article about using join in LINQ with C#.

 

 

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview