Between/Timerange LINQ

Posted by dezza on Stack Overflow See other posts from Stack Overflow or by dezza
Published on 2011-03-10T14:03:24Z Indexed on 2011/03/11 8:10 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

My intention here is to select all entries (Bookings) between "begin" (begin_prefix) and "end" (end_prefix)

BUT! The important thing is:

If I have a booking at 07:25-10:00 - you query for 09:00-10:00 it should still show the booking because it reserves the room until 10 no matter what ..

So ..

07.25-10.00 booking means query for 09:00-10.00 still returns a list of bookings within 09:00-10.00 (which means 07.25-10.00 is included)

        public static List<booking> Today(DateTime begin, DateTime end)
        {
        try
        {
            IFormatProvider Culturez = new CultureInfo(ConfigurationManager.AppSettings["locale"].ToString(), true);
            DateTime begin_prefix = DateTime.ParseExact(begin.ToString(), "dd-MM-yyyy HH:mm:ss", Culturez);
            DateTime end_prefix = DateTime.ParseExact(end.ToString(), "dd-MM-yyyy HH:mm:ss", Culturez);

            dbDataContext db = new dbDataContext();

            // gives bookings BEFORE begin_prefix (why?)
            IQueryable<booking> bQ = from b in db.bookings
              where begin_prefix >= b.Starts &&
              b.Ends <= end_prefix &&
              b.Ends > b.Starts &&
              b.pointsbookings.Count > 0
              select b;
            // ^gives bookings BEFORE begin_prefix (why?)

            List<booking> bL = bQ.ToList();

            return bL;
        }
        catch (Exception)
        {
            throw;
        }
    }

I've tried getting this right for some time now .. Seems everytime I correct it to something new, a new overlap or selection outside the two begin/end dates seem to appear :(

UPDATE CRITERIA and SOURCE: Bookings has to be WITHIN "begin_prefix" and "end_prefix" or on the exact same time ..

.. currently the above code gives me bookings BEFORE begin_prefix date, which is not intentioned! We're in 2011, I got bookings from 2010 as well! **

NEW!! UPDATED:

This is what I have:

SEARCH.START >= BOOKING.START

BOOKING.END <= SEARCH.END

... the problem comes up when ..

BOOKING entry: 10:00(Start)-14:00(End)

This means according to above: 08.59 >= 10.00 (SEARCH.START >= BOOKING.START)

It will never include it. But it should, since this is the same room and the seats are booked individually!

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ