这是我第一次使用LINQ进行外连接。我认为我的查询很接近,但它返回的行数比应该多了很多。 LINQ查询创建的SQL返回2522条记录,而下面显示的sql等效项仅返回8,这是正确的数字。 linq查询中的iqid由常量设置,以便为我的测试用例隔离这一条记录。
我还想知道为什么在where语句中的两个bool评估不起作用。第一个是,但其他两个被注释掉,因为他们不会编译。我尝试使用" =="像第一个语句和0而不是假,但都没有工作。
此查询有什么问题?
的LINQ:
public const string iqid = "9C01FFAE9B4E43D7853D811C8CF77B09";
var outerJoinQuery = from n in context.VCNOTE
// join the other table in and project into IEnumerable "noteGroup"
join l in context.VCNOTELINKS on n.IQID equals l.NOTE into noteGroup
from l in noteGroup.DefaultIfEmpty()
join c in context.SFACONTACT on l.CONTACT equals c.FIRSTNAME into contactGroup
from c in contactGroup.DefaultIfEmpty()
join f in context.VCFUND on l.FUND equals f.IQID into fundGroup
from f in fundGroup.DefaultIfEmpty()
join d in context.VCPROJECT on l.PROJECT equals d.LIBELLE into dealGroup
from d in dealGroup.DefaultIfEmpty()
join a in context.SFAACCOUNT on l.ACCOUNT equals a.NAME into companyGroup
from a in companyGroup.DefaultIfEmpty()
where n.IQID == iqid // && n.IQDELETED = false && l.IQDELETED = false
// project to an anonymous type and set properties to be displayed
select new
{
NoteID = l.NOTE,
FundID = l.FUND,
FundName = f.FUND,
ContactID = l.CONTACT,
ContactName = c.FIRSTNAME,
CompanyID = l.ACCOUNT,
CompanyName = a.NAME,
DealID = d.IQID,
DealName = d.LIBELLE
};
这就是我创建的sql(不是由linq查询生成的)看起来像返回8行并且是预期的结果。这也是linq创建的sql应该是这样的:
select L.[IQID]
,L.[IQDELETED]
,L.[NOTE] 'Linked NoteID'
,L.[FUND] 'Linked FundID'
,F.Fund 'Linked Fund'
,L.[ACCOUNT] 'Linked CompanyID'
,A.NAME 'Linked Company'
,L.[PROJECT] 'Linked DealID'
,D.LIBELLE 'Linked Deal'
,L.[CONTACT] 'Linked ContactID'
,CONCAT(C.FIRSTNAME,' ',C.LASTNAME) 'Linked Contact'
FROM VCNOTELINKS L
LEFT OUTER JOIN VCFUND F ON L.FUND = F.IQID
LEFT OUTER JOIN SFAACCOUNT A ON L.ACCOUNT = A.IQID
LEFT OUTER JOIN VCPROJECT D ON L.PROJECT = D.IQID
LEFT OUTER JOIN SFACONTACT C ON L.CONTACT = C.IQID
where L.note = '9C01FFAE9B4E43D7853D811C8CF77B09' AND L.IQDELETED = 0