我试图根据传递给函数的变量结果,向linq查询添加其他where子句。

var allFeedback =
    from f in _unitOfWork.Feedback.All()
    join b in _unitOfWork.Bookings.All() on f.CourseBookingID equals b.CourseBookingID
    join cb in _unitOfWork.CourseBookings.All() on f.CourseBookingID equals cb.CourseBookingID
    where b.SiteID == siteID && b.Date >= fromDate && b.Date <= to && b.CancelledID == null
    select f;

if (courseID > 0)
{
    allFeedback.Where(f => f.CourseBooking.CourseID == courseID);
}

if (facilitatorID == 0)
{
    allFeedback.Where(f => f.CourseBooking.FacilitatorID == null);
}
else if (facilitatorID > 0)
{
    allFeedback.Where(f => f.CourseBooking.FacilitatorID == facilitatorID);
}

allFeedback.ToList();


我想将where子句添加到原始查询“ allFeedback”中,但是执行查询时,其他子句将被忽略。

这可能吗?

最佳答案

是的,可以这样做:

if (courseID > 0)
{
    allFeedback = allFeedback.Where(f => f.CourseBooking.CourseID == courseID);
}

if (facilitatorID == 0)
{
    allFeedback = allFeedback.Where(f => f.CourseBooking.FacilitatorID == null);
}
else if (facilitatorID > 0)
{
    allFeedback = allFeedback.Where(f => f.CourseBooking.FacilitatorID == facilitatorID);
}


您只是忘记了将结果分配给变量。

10-08 06:11