如何通过linq在join语句中使用两个条件?

时间:2016-07-19 09:05:27

标签: c# linq

我尝试为我的加入添加两个条件,但我不能。我使用||运算符。

from pay in db.Payments
join FlyRpt in db.FlightReportTickets 
on pay.ObjectIdDepartue equals FlyRpt.DepartureId ||
   FlyRpt.DepartureId equals pay.ObjectIdReturn

join prof in db.Profiles on FlyRpt.UserId equals prof.UserId
where pay.ReserveType == 1 &&
    pay.Transactionsuccess &&
    pay.IssueDate >= fromDateTime &&
    pay.IssueDate <= toDateTime

select new
{
    FlyRpt.FullPrice
};

2 个答案:

答案 0 :(得分:2)

如果要在join中实现OR,则需要使用where:

from pay in db.Payments
from FlyRpt in db.FlightReportTickets
where pay.ObjectIdDepartue == FlyRpt.DepartureId
|| FlyRpt.DepartureId == pay.ObjectIdReturn
join prof in db.Profiles on FlyRpt.UserId equals prof.UserId
where pay.ReserveType == 1 &&
pay.Transactionsuccess &&
pay.IssueDate >= fromDateTime &&
pay.IssueDate <= toDateTime
select new
{
    FlyRpt.FullPrice,
};

答案 1 :(得分:1)

您可以使用交叉联接,因为您无法在其他联接中使用OR

from pay in db.Payments
from FlyRpt in db.FlightReportTickets 
where 
    pay.ObjectIdDepartue = FlyRpt.DepartureId ||
    FlyRpt.DepartureId equals pay.ObjectIdReturn
join prof in db.Profiles 
on FlyRpt.UserId equals prof.UserId
where 
    pay.ReserveType == 1 &&
    pay.Transactionsuccess &&
    pay.IssueDate >= fromDateTime &&
    pay.IssueDate <= toDateTime
select new
{
    FlyRpt.FullPrice,
};