Как перевести этот запрос sql в запрос linq, пожалуйста?
USE [BiblioDB]
GO
SELECT *
FROM Exemplaire e
LEFT JOIN location l
ON e.ExemplaireId = l.ExemplaireId
INNER JOIN Retour r
ON l.LocationId is null or l.LocationId = r.LocationId
Go
Я попробовал этот linq-запрос, но четвертая строка не работает.
BiblioDBContext biblioDBContext = new BiblioDBContext();
var query = from e in biblioDBContext.Exemplaires
join l in biblioDBContext.Locations on e.ExemplaireId equals l.LocationId
join r in biblioDBContext.Retours on l.LocationId == null || l.LocationId equals r.LocationId r.LocationId
select e;
Попробуй это:
BiblioDBContext biblioDBContext = new BiblioDBContext();
var query = from e in biblioDBContext.Exemplaires
join l in biblioDBContext.Locations on e.ExemplaireId equals l.ExemplaireId into el
from l in el.DefaultIfEmpty()
from r in biblioDBContext.Retours
where (l.LocationId == null) || (l.LocationId.Equals(r.LocationId))
select new {e, l, r};
Проблема заключается в отсутствующем DefaultIfEmpty()
, которое необходимо для левых объединений.