Linq-to-sql integrates with several of the sentence syntax and traditional join syntax

What is the difference between writing a connection using 2 from and a where clauses as follows:

 var SomeQuery = from a in MyDC.Table1 from b in MyDC.Table2 where a.SomeCol1 == SomeParameter && a.SomeCol2 === b.SomeCol1 

and writing a join using the join operator.

This is for join on 2 tables, but of course, sometimes we need to join even more tables, and we need to combine other from clauses with where , if we choose the syntax above.

I know that both syntax queries return the same data, but I was wondering if there was a performance difference or some other difference that would finally approve one syntax over another.

Thanks for your suggestions.

+6
source share
2 answers

This question is actually very good in these two.

INNER JOIN ON vs WHERE clause

INNER JOIN and several table names in the FROM clause

I included two examples of how three different LINQ expressions will be translated into SQL.

Implicit join:

 from prod in Articles from kat in MainGroups where kat.MainGroupNo == prod.MainGroupNo select new { kat.Name, prod.ArticleNo } 

Will be translated into

 SELECT [t1].[Name], [t0].[ArticleNo] FROM [dbo].[Article] AS [t0], [dbo].[MainGroup] AS [t1] WHERE [t1].[MainGroupNo] = [t0].[MainGroupNo] 

Internal connection:

 from prod in Articles join kat in MainGroups on prod.MainGroupNo equals kat.MainGroupNo select new { kat.Name, prod.ArticleNo } 

Will be translated into

 SELECT [t1].[Name], [t0].[ArticleNo] FROM [dbo].[Article] AS [t0] INNER JOIN [dbo].[MainGroup] AS [t1] ON [t0].[MainGroupNo] = [t1].[MainGroupNo] 

Left outer join:

 from prod in Articles join g1 in MainGroups on prod.MainGroupNo equals g1.MainGroupNo into prodGroup from kat in prodGroup.DefaultIfEmpty() select new { kat.Name, prod.ArticleNo } 

Will be translated into

 SELECT [t1].[Name] AS [Name], [t0].[ArticleNo] FROM [dbo].[Article] AS [t0] LEFT OUTER JOIN [dbo].[MainGroup] AS [t1] ON [t0].[MainGroupNo] = [t1].[MainGroupNo] 

If you want to check how your expressions will be translated into SQL, I recommend that you try LINQPad . This is a terrific tool for figuring out these kinds of things.

+9
source
 var result = from a in DB.classA from b in DB.classB where a.id.Equals(b.id) select new{ab}; 
-2
source

Source: https://habr.com/ru/post/921804/


All Articles