当1个或多个用户保存相同产品时,左联接返回重复的产品。
我解决了SQL查询中的问题。这是一个查询:
select p.ProductID,
(case when c.UserID = 3 then 'true' else 'false' end) as flag
from product as p
left join SavedItem as c on product.ProductID = c.ProductID and
c.UserID = 3
无法弄清楚如何在实体框架中进行操作。
left join SavedItem as c on product.ProductID = c.ProductID and
c.UserID = 3
左联接可以这样解决:
join c in SavedItem on p.ProductID equals c.ProductID into lj
from c in lj.DefaultIfEmpty()
c.UserID = 3
放在哪里?
答案 0 :(得分:0)
尝试一下:
var query= from product in context.Products
from SavedItem in context.SavedItems.Where(c=> c.ProductID = product.ProductID && c.UserID == 3).DefaultIfEmpty()
select new {
ProductID=product.ProductID,
Flag=(SavedItem==null || SavedItem.UserID != 3) ? false : true
};
答案 1 :(得分:-1)
在这里!我找到了解决方法。
from product in context.Products
join c in context.SavedItems
on new { p1 = (int?)product.ProductID , p2 = (int?)cat.UserID }
equals new { p1 = c.ProductID ,p2 = c.UserID} into lj
from c in lj.DefaultIfEmpty()
select new{
...
flag = (c.UserID == cat.UserID ? "true" : "false"),
...
}