Linq加入where子句不起作用

时间:2015-04-16 14:24:11

标签: c# sql-server linq join where

我有这个mssql查询,我想转换为linq

select * from personas
left join PersonaDocumento on PersonaDocumento.PersonaId=personas.Id
where personas.FirstName='23895389' or PersonaDocumento.Documento='23895389'

这不起作用:

from p in db.personas
join d in db.PersonaDocumento on p.Id equals d.PersonaId into dj1
where p.FirstName=='23895389' || dj1.Documento=='23895389'
from d in dj1.DefaultIfEmpty()
select p

2 个答案:

答案 0 :(得分:0)

我认为你在那里犯了三个错误:

from p in db.personas
join d in db.PersonaDocumento on p.Id equals d.PersonaId into dj1
where p.FirstName=='Jzuan' 
from d in dj1.DefaultIfEmpty()
where d.Documento=='23895389' 
select d 

首先是join d on,正确的语法是in。第二,你是从p中选择而不是从你加入的表d中选择的。 加dj1是一个元素列表,所以如果你需要过滤元素,你应该在之后进行。具体来自d dj1条款中的{{1}}

答案 1 :(得分:0)

试试这个:

from p in db.personas
join d in db.PersonaDocumento on p.Id equals d.PersonaId into dj1
from ld in dj1.DefaultIfEmpty()
where p.FirstName=='Jzuan' || (ld != null && ld.Documento=='23895389')
select p

注意我如何重新排序条款。