這個問題其實在這兩個答案中很不錯。
INNER JOIN ON vs WHERE clause
INNER JOIN vs multiple table names in "FROM"
我將在三個不同的LINQ表達式將如何轉換成SQL兩個例子。
隱式連接:
from prod in Articles
from kat in MainGroups
where kat.MainGroupNo == prod.MainGroupNo
select new { kat.Name, prod.ArticleNo }
將被翻譯成
SELECT [t1].[Name], [t0].[ArticleNo]
FROM [dbo].[Article] AS [t0], [dbo].[MainGroup] AS [t1]
WHERE [t1].[MainGroupNo] = [t0].[MainGroupNo]
內連接:
from prod in Articles
join kat in MainGroups on prod.MainGroupNo equals kat.MainGroupNo
select new { kat.Name, prod.ArticleNo }
將被翻譯成
SELECT [t1].[Name], [t0].[ArticleNo]
FROM [dbo].[Article] AS [t0]
INNER JOIN [dbo].[MainGroup] AS [t1] ON [t0].[MainGroupNo] = [t1].[MainGroupNo]
LEFT OUTER JOIN:如果你想測試你的表情會如何轉換成SQL
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 }
將被翻譯成
SELECT [t1].[Name] AS [Name], [t0].[ArticleNo]
FROM [dbo].[Article] AS [t0]
LEFT OUTER JOIN [dbo].[MainGroup] AS [t1] ON [t0].[MainGroupNo] = [t1].[MainGroupNo]
,我建議您嘗試LINQPad 。這是一個很好的工具來搞清楚這種東西。
那麼,有什麼好? 2個或更多從條款或2個或更多的連接條款? – frenchie 2012-07-31 23:35:57
根據http://stackoverflow.com/questions/894490/sql-left-join-vs-multiple-tables-on-from-line加入條款似乎更可取。 – 2012-07-31 23:40:29