我有两张桌子,例如promotionalOffers和PromotionalOffersUsed
我现在要通过联接获取匹配记录,我想包含不同的条件,例如PromotionalOffers.ISVCSPId = 10和PromotionalOffersUsed.OfferId为null

我已经使用预期的左连接在sql中编写了查询,但无法在linq中编写相同的查询

这是我的SQL查询

SELECT *
FROM PromotionalOffers
left JOIN PromotionalOffersUsed ON PromotionalOffers.Id = PromotionalOffersUsed.OfferId where PromotionalOffers.ISVCSPId =10 and PromotionalOffersUsed.OfferId is null


我的linq查询是

(from s in db.PromotionalOffers
 join e in db.PromotionalOffersUsed on s.Id equals e.OfferId
 where s.ISVCSPId == iSVCSPData.Id
 select s).ToListAsync();


我无法在此处加入左加入

最佳答案

这样尝试;

(from s in db.PromotionalOffers
 join e in db.PromotionalOffersUsed on s.Id equals e.OfferId into joinT
 from e in joinT.DefaultIfEmpty()
 where s.ISVCSPId == 10 && (e == null || e.OfferId == null)
 select new { PromotionalOffers = s, PromotionalOffersUsed = joinT } ).ToListAsync();

关于c# - linq:在linq中具有不同类型条件的左联接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47344042/

10-10 05:17