linq-to-sql连接多个from子句语法与传统连接语法

时间:2021-07-10 10:05:11

What the difference between writing a join using 2 from clauses and a where like this:

使用2从子句和类似的地方写一个连接有什么区别:

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.

并使用join运算符编写连接。

This is for a 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.

这是针对2个表的连接,当然,有时候,我们需要连接更多的表,我们需要将其他子句与where(如果我们选择上面的语法)结合起来。

I know both syntax queries return the same data but I was wondering if there's a performance difference, or another kind of difference, that would conclusively favor one syntax over the other.

我知道两种语法查询都返回相同的数据,但我想知道是否存在性能差异或其他类型的差异,最终会使一种语法优于另一种语法。

Thanks for your suggestions.

谢谢你的建议。

2 个解决方案

#1


14  

This question is actually answered pretty good in these two.

这两个问题实际上回答得很好。

INNER JOIN ON vs WHERE clause

INNER JOIN ON vs WHERE子句

INNER JOIN vs multiple table names in "FROM"

INNER JOIN vs“FROM”中的多个表名

I've included two examples on how three different LINQ expressions will be translated into SQL.

我已经包含了两个关于如何将三种不同的LINQ表达式转换为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]

Inner join:

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 test how your expressions will be translated into SQL, I recommend that you try LINQPad. It's an awesome tool for figuring out this kind of stuff.

如果您想测试表达式如何转换为SQL,我建议您尝试LINQPad。这是一个很好的工具,可以找出这种东西。

#2


-2  

var result = from a in DB.classA
from b in DB.classB
where a.id.Equals(b.id)
select new{a.b};

#1


14  

This question is actually answered pretty good in these two.

这两个问题实际上回答得很好。

INNER JOIN ON vs WHERE clause

INNER JOIN ON vs WHERE子句

INNER JOIN vs multiple table names in "FROM"

INNER JOIN vs“FROM”中的多个表名

I've included two examples on how three different LINQ expressions will be translated into SQL.

我已经包含了两个关于如何将三种不同的LINQ表达式转换为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]

Inner join:

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 test how your expressions will be translated into SQL, I recommend that you try LINQPad. It's an awesome tool for figuring out this kind of stuff.

如果您想测试表达式如何转换为SQL,我建议您尝试LINQPad。这是一个很好的工具,可以找出这种东西。

#2


-2  

var result = from a in DB.classA
from b in DB.classB
where a.id.Equals(b.id)
select new{a.b};