Working With Inner Join Using LINQ And Lambda

Inner Join

Inner join returns only those records or rows that match or exist in both tables. In other words, it gives a common pattern from both tables.

SQL Syntax

SELECT column_name(s)  
FROM table1  
INNER JOIN table2 ON table1.column_name = table2.column_name;  

Now design the tables and insert the dummy records as below

Inner Join Using LINQ And Lambda 

Now Integrate database tables to your application using Entity framework database first approach.

Inner Join Using LINQ And Lambda

Here CSharpCornerEntities is the Entity framework context name. Now let us see the example using these tables

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
namespace InnerJoin {  
    class Program {  
        static void Main(string[] args) {  
            using(CSharpCornerEntities databaseEntities = new CSharpCornerEntities()) {#  
                region InnerJoin with Linq  
                //deffered query exection  
                var linqQuery = from emp in databaseEntities.Employees  
                join dep in databaseEntities.Departments  
                on emp.ID equals dep.EmpID  
                select new {  
                    emp.ID,  
                        emp.Name,  
                        dep.DepartmentName  
                };  
                Console.WriteLine("\n\t Employee Data with Inner join Using Linq Query");  
                //immediate query execution  
                foreach(var empData in linqQuery) {  
                    Console.WriteLine("ID : " + empData.ID + ", Name : " + empData.Name + ", Department : " + empData.DepartmentName);  
                }#  
                endregion# region InnerJoin with Lambda  
                //deffered query exection  
                var LambdaQuery = databaseEntities.Employees.Join(databaseEntities.Departments, e => e.ID, d => d.EmpID, (e, d) => new {  
                    e.ID, e.Name, d.DepartmentName  
                });  
                Console.WriteLine("\n\t Employee Data with Inner join Using Linq Lambda");  
                //immediate query execution  
                foreach(var empData in LambdaQuery) {  
                    Console.WriteLine("ID : " + empData.ID + ", Name : " + empData.Name + ", Department : " + empData.DepartmentName);  
                }#  
                endregion  
            }  
        }  
    }  
}

Now run your application.

Inner Join Using LINQ And Lambda

I hope it's helpful