2014-02-20 56 views
1

我應該產生一個通用的左外LINQ的聲明,多數民衆贊成加入(特別是與空),而是它產生了奇怪的sql也使用COUNT(*)LINQ的是生產與左外意外SQL連接

這是LINQ:

(from ccc in cDataContext.CategoryCountryCategoryTypeMappings 
join cl in currentLogs on ccc.CategoryCountryCategoryTypeMappingID equals 
cl.CategoryCountryCategoryTypeMappingID 
into final 
select final); 

當前日誌看起來是這樣的:

(from dll in cDataContext.DownloadLogs 
    where dll.DTS.Hour == DateTime.Now.Hour && dll.DTS.Date== DateTime.Now.Date 
    select dll) 

輸出SQL變爲:

SELECT [t1].[LogId], [t1].[CategoryCountryCategoryTypeMappingID], [t1].[CaptureTime], [t1].[Response], [t1].[DTS], [t1].[DLID], (
    SELECT COUNT(*) 
    FROM clients.[dbo].[DownloadLog] AS [t2] 
    WHERE ([t0].[CategoryCountryCategoryTypeMappingID] = [t2].[CategoryCountryCategoryTypeMappingID]) 
) AS [value] 
FROM clients.[Store].[CategoryCountryCategoryTypeMappings] AS [t0] 
LEFT OUTER JOIN clients.[dbo].[DownloadLog] AS [t1] ON ([t0].[CategoryCountryCategoryTypeMappingID] = [t1].[CategoryCountryCategoryTypeMappingID]) 
ORDER BY [t0].[CategoryCountryCategoryTypeMappingID], [t1].[LogId] 

尋找什麼IM:

SELECT * FROM 
clients.[Store].[CategoryCountryCategoryTypeMappings] AS [t0] 
LEFT INNER JOIN clients.[dbo].[DownloadLog] AS [t1] 
ON ([t0].[CategoryCountryCategoryTypeMappingID] = [t1].[CategoryCountryCategoryTypeMappingID]) 

我如何產生所需的SQL?

回答

1

MSDN有很好的linq例子。 http://msdn.microsoft.com/en-US/vstudio/ee908647.aspx

對於左外連接:

var q = 
    from c in categories 
    join p in products on c equals p.Category into ps 
    from p in ps.DefaultIfEmpty() 
    select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName }; 
+0

尋找左內連接 –

+0

@Ukemi:「什麼我找」您的文章的部分說,你正在尋找一個「左外連接」。這是什麼? – uhleeka

+0

這是我的錯誤。 –