0

I'm trying convert this SQL to Entity Framework LINQ, but don't working.

My SQL code:

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

My Entity Framework Code:

 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),
              };
4

2 回答 2

0

我认为您应该使用导航属性。导航属性用于导航数据中的关系。

于 2013-05-03T14:15:37.190 回答
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),
              };
于 2013-05-06T10:58:26.623 回答