2013-05-03 150 views
0

我想將此SQL轉換爲實體框架LINQ,但不工作。Left加入實體框架

我的SQL代碼:

SELECT 
    s.Id, 
    s.OriginalPhotoBlobId, 
    s.PhotoBlobExtension, 
    ISNULL(s.ProductSkuKey, p.[Key]) as [Key], 
    p.Name, 
    ISNULL(sp.Price, 0) as [Price], 
    sp.PriceList_Id 
FROM SKUs s 
INNER JOIN Products p on p.Id = s.Product_Id 
LEFT JOIN SKUPrices sp on sp.SKU_Id = s.Id 

我的實體框架代碼:

var db = this.Context; 
var prices = from s in db.SKUs 
       join p in db.Products on s.Product equals p 
       join sp in db.SKUPrices on s equals sp.SKU into gj 
       from spss in gj.DefaultIfEmpty() 
       select new PriceListItem 
       { 
       Id = s.Id, 
       BlobId = s.OriginalPhotoBlobId, 
       BlobExtension = s.PhotoBlobExtension, 
       Key = ((s.ProductSkuKey == null || s.ProductSkuKey.Trim() == string.Empty) ? p.Key : s.ProductSkuKey), 
       Name = p.Name, 
       Price = (spss == null ? default(double) : spss.Price), 
       }; 
+0

你能解釋一下你正在試圖通過精確地轉化這個達到什麼樣的目標?例如,通過使用include,您不必使用像這樣的連接。 – 2013-05-03 14:10:36

+0

你能解釋一下_你的工作_是什麼意思嗎? – 2013-05-03 14:34:48

回答

0

我認爲你應該使用navigation性能。導航屬性用於導航數據中的關係。

0

試着加入的屬性而不是類

var prices = from s in db.SKUs 
       join p in db.Products on s.Product_Id equals p.Id 
       join sp in db.SKUPrices on sp.SKU_Id = s.Id into gj 
       from sp in gj.DefaultIfEmpty() 
       select new PriceListItem 
       { 
       Id = s.Id, 
       BlobId = s.OriginalPhotoBlobId, 
       BlobExtension = s.PhotoBlobExtension, 
       Key = ((s.ProductSkuKey == null || s.ProductSkuKey.Trim() == string.Empty) ? p.Key : s.ProductSkuKey), 
       Name = p.Name, 
       Price = (sp == null ? default(double) : sp .Price), 
       };