Search code examples
c#asp.netentity-frameworklinqdata-access-layer

Select where not exist in interval ? Different between four dates


In the first the business is car rental system .

I want to get all car where has no orders in interval selected by user

public List<Car> SearchCar(DateTime pickdate, DateTime dropdate)
{
    var db = new CarRentalDBEntities();
    var temp = new List<Car>();

    temp = db.Cars.Where(item => 

       !item.Orders.Any
        (e => e.PickUpDateTime >= pickdate && dropdate  <= e.DropDataTime)

        ).ToList();

    return temp;
}

this is the last code I write

  • the error is : cars still comes if order intersect with interval user choosed

Solution

  • As per my comment, you probably want to be checking if either the pickup date is in the range or the return date is in the range, or if the rental period is longer than the entire range:

    e => (pickdate <= e.PickUpDateTime && e.PickUpDateTime < dropdate) || //picked up in period
         (pickdate <= e.DropDataTime && e.DropDataTime < dropdate) || //dropped off in period
         (e.PickUpDateTime < pickdate && e.DropDataTime > dropdate) //rental spans period
    

    Notes: typo in your DropDataTime