C# 左连接实体框架
我正在尝试将此SQL转换为实体框架LINQ,但不起作用 我的SQL代码:C# 左连接实体框架,c#,sql,linq,frameworks,entity,C#,Sql,Linq,Frameworks,Entity,我正在尝试将此SQL转换为实体框架LINQ,但不起作用 我的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 Produ
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),
};
我认为你应该使用属性。导航属性用于在数据中的关系中导航。尝试加入属性而不是类
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),
};
你能准确地解释一下你想要达到的目标吗?例如,通过使用include,您不必像这样使用连接。您能解释一下“不工作”是什么意思吗?