I have a method like this:
public ActionResult ShowAvailableSpots(int Id, DateTime ArrivalDate, DateTime LeaveDate)
{
var query2 = db.Spots
.Where(c => db.Reservations.Any(r =>
DbFunctions.TruncateTime(ArrivalDate) <= DbFunctions.TruncateTime(r.ArrivalDate) && DbFunctions.TruncateTime(LeaveDate) <= DbFunctions.TruncateTime(r.ArrivalDate)
|| DbFunctions.TruncateTime(ArrivalDate) >= DbFunctions.TruncateTime(r.LeaveDate)
)).ToList();
ViewBag.StartingDate = ArrivalDate;
ViewBag.EndingDate = LeaveDate;
ViewBag.AvailableSpots = query2;
ViewBag.CampingSpotId = new SelectList(query2, "CampingSpotId", "SpotName");
return View();
}
It determines wether any of the reservations match the date criteria. If they don't match, then the list with Campingspots is returned.
The problem is, that it is returning ALL spots or NONE spots instead of just the spots that are available. This is due to the .Any method. How can I filter out the campingspots that are not available?
Try something like this:
The inner
Where
statement says we're only checking the reservations that apply to that camping spot, and theAll
statement checks to make sure that every reservation for that campsite is outside the window we're interested in.