Linq:加入多种条件

时间:2017-06-20 14:35:25

标签: linq join

我正在尝试加入多种条件。问题在于它是'等于'和'不等于'的混合体。此previous answer仅在您想加入'等于'时才有效。

from p1 in context.Set<PersonList>()
join p2 in context.Set<PersonList>()
on p1.Email equals p2.Email && p1.PersonID != p2.PersonID

1 个答案:

答案 0 :(得分:0)

只需使用where来捕获额外条件:

from p1 in context.Set<PersonList>()
join p2 in context.Set<PersonList>()
on p1.Email equals p2.Email into p2j
from p2 in p2j.DefaultIfEmpty()
where p2 != null && p1.PersonID != p2.PersonID