Linq,ASP.. NET MVC:如何使用"和“;equals"在join语句中

本文关键字:quot equals join 语句 MVC 何使用 ASP Linq NET | 更新日期: 2023-09-27 18:19:23

如何在linq查询中翻译这个sql查询,请?

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;

Linq,ASP.. NET MVC:如何使用"和“;equals"在join语句中

试试这个:

 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()子句。