INNER JOINS
Inner join returns only row which exists in both the tables.That means inner join only returns common records between two table.
Now I want to perform the inner joins between Teachers and Department table.
Using Sql query:
select t.NAME,d.DepName from Teacher t
inner join Department d on t.Dep=d.Depid
Linq Query
LinqtoSqlDbDataContext db = new LinqtoSqlDbDataContext();
var result = from t in db.Teachers
join d in db.Departments on t.Dep equals d.Depid
select new {
t.Name,
d.DepName
};
Linq Joins on multiple table
Sql Query:-
select t.Name,d.DepName,a.Adressline2,c.CountryName from Teacher t
inner join Department d on t.Dep=d.Depid
inner join Adressstbl a on t.Adress=a.Addid
inner join Countrytbl c on a.Countryid=c.id
Linq Query:-
LinqtoSqlDbDataContext db = new LinqtoSqlDbDataContext();
var result = from t in db.Teachers
join d in db.Departments on t.Dep equals d.Depid
join a in db.Adressstbls on t.Adress equals a.Addid
join c in db.Countrytbls on a.Countryid equals c.id
select new {
t.Name,
d.DepName,
a.Adressline2,
c.CountryName
};
LEFT JOIN
LEFT JOIN or LEFT OUTER JOIN
LEFT JOIN returns all rows from left table and from right table returns only common or matched records.
If there are no columns matching in the right table, it returns NULL values.
Sql Query:
select t.Name,d.DepName from Teacher t
left join Department d on t.Dep=d.Depid
Linq Query:
LinqtoSqlDbDataContext db = new LinqtoSqlDbDataContext();
var result = from t in db.Teachers
join d in db.Departments on t.Dep equals d.Depid into td
from dt in td.DefaultIfEmpty()
select new {
t.Name,
dt.DepName,
Left join on multiple tables
Sql Query:
select t.Name,d.DepName,a.Adressline2,c.CountryName from Teacher t
left join Department d on t.Dep=d.Depid
left join Adressstbl a on t.Adress=a.Addid
left join Countrytbl c on a.Countryid=c.id
Linq Query:
LinqtoSqlDbDataContext db = new LinqtoSqlDbDataContext();
var result = from t in db.Teachers
join d in db.Departments on t.Dep equals d.Depid into td
from dt in td.DefaultIfEmpty()
join a in db.Adressstbls on t.Adress equals a.Addid into ta
from at in ta.DefaultIfEmpty()
join c in db.Countrytbls on at.Countryid equals c.id into ac
from ca in ac.DefaultIfEmpty()
select new {
t.Name,
dt.DepName,
at.Adressline2,
ca.CountryName
};
Right JOIN :-
REIGHT JOIN returns all rows from right table and from left table returns only matched records.
If there are no columns matching in the left table, it returns NULL values.
Sql Query:
select t.Name,d.DepName from teacher t
right join department d on t.Dep=d.Depid
Linq Query:
For right join just swap the table
LinqtoSqlDbDataContext db = new LinqtoSqlDbDataContext();
var result = from t in db.Departments
join d in db.Teachers on t.Depid equals d.Dep into td
from dt in td.DefaultIfEmpty()
select new {
dt.Name,
t.DepName
};
The post INNER JOIN,RIGHT JOIN,LEFT JOIN USING LINQ IN Multiple Table C# appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- How to get two table value using Linq
- Linq join two string list using contains
- Query a table by using foreign key navigation properties in LINQ with C#
- Update table using Linq and Extension Method
- Inner join returning more than 1 row from second table
- Server evaluated search for multiple keywords using linq and c#
- How to pass multiple Excel sheets as data table when using ExcelDataReader?
- Using a LINQ Where clause to filter by data in an associated table
- Update multiple table at the same time using stored procedure
- Syntax Error (Missing Operator) in query expression with Multiple Inner Join in C# MS Access
- Using Database first approach in Entity framework, how to fetch the data from a single table only which has multiple foreign key relationship?
- Trying to use Join to get data from two table into one query search using Join
- MySql Inner join of 2 table with WHERE Clauses
- LINQ inner join condition
- Improve linq query speed using join
- LINQ to Sql performance using multiple LIKE's vs using a List
- How to retrieve an element with specific attribute using LINQ to XML with multiple elements with same name?
- How to insert a row from one table to another table using Linq to SQL
- Insert multiple child records using linq to sql
- Will updating different rows from same table using multiple threads cause any deadlocks or delay in updates due to table/page locks?
- LINQ with multiple left join and where clause
- Using VARCHAR(Max) (That is really a string []) in an Inner Join using IN
- Using MySql inner join to show data on google piechart
- Adding records in multiple table using EF6 code first
- Using an SQL INNER JOIN with OleDbDataReader in C#
- Linq Lookup using multiple conditions
- Error while using INNER JOIN in C# Access
- How to count multiple cell values individually and then assign to a list using linq
- SQL Join Multiple table query
- Linq SQL join on same table with group and sum
- How can I dynamically add and remove ASP:DropDownLists?
- How to enter Razor within Javascript without quotes in ASP.NET MVC
- Efficient way to get all elements from a HashSet in .NET
- Can't convert my timespan difference to any of the custom ones
- Advice neaed: a best way to write binary data to pipe
- How to get current Test Case ID in the mtm for data driven test
- SQL Server Reporting Services Incompatible
- Windows Phone 8.1 open link in other app
- classes and threading
- Comparision could not compare C#
- UnauthorizedAccessException when creating XML file
- .NET regex match returning too many elements
- Load xml row into 2D array
- Which style is the better and efficiency way to export csv file
- Best way to share code between multiple MVC applications and deploy different versions
- Sending file in C#
- How can i pass my values in e-mail activation link?
- Code Contracts and ccrewrite
- Formula to flatten array of grid with negative values
- Prevent the entering of users that have entered the wrong password more than 3 times