2

I'm trying to add additional where clauses to a linq query depending on what variable results are passed to a function.

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();

I want to add the where clauses to the original query "allFeedback" but when the query is executed the additional clauses are ignored.

Is this possible?

1 Answer 1

6

yes its possible just do :

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

You just forgot to assign the result to the variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Amazing thank you so much! :) I'll mark this as the accepted answer when it allows me, thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.