SQL Joins with C# LINQ
http://www.dotnettricks.com/learn/linq/sql-joins-with-csharp-linq
There are Different Types of SQL Joins which are used to query data from more than one tables. In this article, I would like to share how joins work in LINQ. LINQ has a JOIN query operator that provide SQL JOIN like behavior and syntax. Let's see how JOIN query operator works for joins. This article will explore the SQL Joins with C# LINQ.
- INNER JOIN
- LEFT OUTER JOIN
- CROSS JOIN
- GROUP JOIN
The JOIN query operator compares the specified properties/keys of two collections for equality by using the EQUALS keyword. By default, all joins queries written by the JOIN keyword are treated as equijoins.
LINQ PAD for running and debugging LINQ Query
I am a big fan of LINQ Pad since it allow us to run LINQ to SQL and LINQ to Entity Framework query and gives the query output. Whenever, I need to write LINQ to SQL and LINQ to Entity Framework query then, I prefer to write and run query on LINQ PAD. By using LINQ PAD, you can test and run your desired LINQ query and avoid the head-ache for testing LINQ query with in Visual Studio. You can download the LINQ Pad script used in this article by using this link.
In this article, I am using LINQ PAD for query data from database. It is simple and useful. For more help about LINQ PAD refer the link. You can download the database script used in this article by using this link. Suppose we following three tables and data in these three tables is shown in figure.
INNER JOIN
Inner join returns only those records or rows that match or exists in both the tables.
C# Code
- var q=(from pd in dataContext.tblProducts join od in dataContext.tblOrders on pd.ProductID equals od.ProductID orderby od.OrderID select new { od.OrderID,
- pd.ProductID,
- pd.Name,
- pd.UnitPrice,
- od.Quantity,
- od.Price,
- }).ToList();
Explained Perfectly, Good Job
ReplyDeleteDOT NET Training in Chennai | DOT NET Course in Chennai