如何通过linq在联接语句中使用两个条件

本文关键字:条件 两个 linq 何通过 语句 | 更新日期: 2023-09-27 17:57:32

我试图向我的联接添加两个条件,但我做不到。我使用||运算符。

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
};

如何通过linq在联接语句中使用两个条件

如果要在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,
};

您可以使用交叉联接,因为您不能在其他联接中使用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,
};