.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Shivakumaran
Pankaj Mishra
alex morco
Imran Ghani
Meenakshi

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Joins in Linq

Posted By:Debendra       Posted Date: January 23, 2016    Points: 200    Category: LINQ    URL: http://www.dotnetspark.com  

Here in this article i am going to discuss various types of Joins in Linq.
 

Joins In LinQ

A Linq JOIN Keyword is used to combine rows from two or more tables, based on a common field between them.Like Sql Joins the Linq is also provided some Keywords to achive Inner Join and outer join.
As we know fron Sql outer join is divided into 2  groups that is Left Outer Join  and Right Outer Join.here in this article we we learn how to achive this joins in Linq.
Let us discuss what is inner join.

INNER JOIN:-Inner Join only return matched records between two or more table based on a commen field between thease table.

LEFT OUTER JOIN: Return all rows from the left table, and the matched rows from the right table

RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table

FULL JOIN: Return all rows when there is a match in ONE of the tables.

Inner Join:
Here in this console application i have two list(EmployeeList and Department List).Considering these 2 list as 2 diffrent tables  i am joining these 2 tables an the basic of a common column i.e ID.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Joins
{
    public class Employee
    {
        public int Id { get; set; }
        public int age { get; set; }
        public string name { get; set; }
        public string gender { get; set; }
    }
    public class Department
    {
        public int id { get; set; }
        public string Departments { get; set; }
        public string Location { get; set; }

    }
    class Demo
    {

    
        static void Main(string[] args)
        {

            List<Employee> li = new List<Employee>();
            li.Add(new Employee { Id = 1, age = 19, name = "Ritesh", gender = "M" });
            li.Add(new Employee { Id = 2, age = 20, name = "sujit", gender = "M" });
            li.Add(new Employee { Id = 3, age = 23, name = "Kabir", gender = "F" });
            li.Add(new Employee { Id = 4, age = 3, name = "mantu", gender = "F" });
            li.Add(new Employee { Id = 5, age = 24, name = "Kamlesh", gender = "M" });
            li.Add(new Employee { Id = 6, age = 28, name = "Manoj", gender = "M" });


            List<Department> Deli = new List<Department>();
            Deli.Add(new Department { id = 2, Departments = "IT", Location = "Bangalore" });
            Deli.Add(new Department { id = 8, Departments = "IT", Location = "Bangalore" });
            Deli.Add(new Department { id = 3, Departments = "HR", Location = "Bangalore" });
            Deli.Add(new Department { id = 7, Departments = "HR", Location = "Bangalore" });
            Deli.Add(new Department { id = 6, Departments = "Account", Location = "Bangalore" });
            var result = from emp in li
                         join
                          de in Deli
                         on emp.Id equals de.id
                         select new
                             {
                                 EmployeeId = emp.Id,
                                 EmployeeName = emp.name,
                                 Department = de.Departments,
                                 Location = de.Location


                             };
                        


            Console.WriteLine(" ID\t\tName\t\t DepartmentName \t\tLocation");
            
            foreach (var obj in result)
            {
                Console.WriteLine(obj.EmployeeId + "\t\t" + obj.EmployeeName + "\t\t" + obj.Department + "\t\t\t\t" + obj.Location);
            }

            

 

            Console.ReadLine();

        }
    }
}

Left Outer Join:

Here for the same table i am writting the left outer join.The code for left outer join is as follow.


  

var result = from emp in li
                         join
                         de in Deli
                         on emp.Id equals de.id into tempstorage
                         from dx in tempstorage.DefaultIfEmpty()
                         select new
                         {
                             EmployeeId = emp.Id,
                             EmployeeName = emp.name,
                             
                             Department = (dx != null) ? dx.Departments : "NULL",
                             Location = (dx != null) ? dx.Departments : "NULL"


                         };


Right Outer Join:

var result = from
                             de in Deli
                             join
                         emp in li
                         
                         
                         on de.id equals emp.Id into tempstorage
                         from dx in tempstorage.DefaultIfEmpty()
                         select new
                         {
                             EmployeeId = (dx != null) ? dx.Id Blush | :O
                             EmployeeName = (dx != null) ? dx.name : "NULL",
                             
                             Department = de.Departments,
                             Location = de.Location



                         };

So in this way we can perform diffrent types of join in  Linq.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

  • Web Service
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend