In LEFT JOIN if all records of the LEFT table are returned by matching RIGHT table. If the RIGHT table is not matched then ‘NULL’ (no value) returns.LINQ is used in C # to query field objects from different types of data in the same way that we use SQL Language to query a Relational Database.

As a query, we retrieve data from Data Source based on any Specific Condition or Criteria and Access and Manipulate that Retrieved Data according to your need. The query can be of different types, but each type of query returns some kind of data collection.

In this post, I will explain Linq joins on multiple tables. I have created two tables i.e,Department, Employee.

 

Table Sql Script:

//Department table
CREATE TABLE [dbo].[Department](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [DepartmentName] [nvarchar](50) NULL,
 CONSTRAINT [PK_Departments] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
//Employee Table

CREATE TABLE [dbo].[Employee](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](150) NULL,
    [Address] [nvarchar](500) NULL,
    [Salary] [decimal](18, 2) NULL,
    [DepId] [int] NULL,
 CONSTRAINT [PK_Employee_1] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]



Linq Left Join With where Clouse .
Sql Query:-


select e.Name,d.DepartmentName from Employee e
LEFT JOIN Department d on e.DepId=d.Id
where e.Address='Paris'



Linq Query:-

var query = from person in db.Employees
                        join dep in db.Departments on person.DepId equals dep.Id into gj
                        from subdata in gj.DefaultIfEmpty()
                        where person.Address== "Paris"
                        select new { person.Name, DepartmentName = subdata.DepartmentName };
            

 

The post [Solved]-LEFT OUTER JOIN in LINQ With Where clause in C# appeared first on Software Development | Programming Tutorials.



Read More Articles