之间/时间范围 LINQ

发布于 2024-10-21 03:18:09 字数 1911 浏览 2 评论 0原文

我的目的是选择“开始”(begin_prefix) 和“结束”(end_prefix) 之间的所有条目(预订)

但是!重要的是:

如果我在 07:25-10:00 进行了预订 - 您查询 09:00-10:00,它仍然应该显示预订,因为它会将房间保留到 10 点无论如何..

所以..

07.25-10.00 预订意味着查询 09:00-10.00 仍然返回 09:00-10.00 内的预订列表(这意味着包括 07.25-10.00)

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

我已经尝试正确解决这个问题有一段时间了..似乎每次我将其纠正为新的内容时,两个开始/结束日期之外的新重叠或选择似乎都会出现:(

更新条件和来源: 预订必须在“begin_prefix”和“end_prefix”内或完全相同的时间..

.. 目前,上面的代码为我提供了begin_prefix 日期之前的预订,这不是我们故意的!我也收到了 2010 年的预订! **

更新:

这就是我的:

SEARCH.START >= BOOKING.START

BOOKING.END <= SEARCH.END

... 时出现问题

当 ..预订条目:10:00(开始)-14:00(结束)

这意味着根据上述: 08.59 >= 10.00 (SEARCH.START >= BOOKING.START)

它永远不会包含它。但应该如此,因为这是同一个房间,而且座位是单独预订的!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

缘字诀 2024-10-28 03:18:09

恕我直言,它应该是这样的:

from b in db.bookings
where begin_prefix <= b.Starts && b.Starts < end_prefix && b.Ends <= end_prefix && bEnds >= b.Starts && b.pointsbookings.Count > 0
select b;

重要的更改是 begin_prefix <= b.Starts 而不是 begin_prefix >= b.Starts。为了确定起见,我还添加了一个检查 bEnds >= b.Starts

顺便说一句:您标记为重要的部分对我来说似乎完全是无稽之谈。

IMHO, it should be like this:

from b in db.bookings
where begin_prefix <= b.Starts && b.Starts < end_prefix && b.Ends <= end_prefix && bEnds >= b.Starts && b.pointsbookings.Count > 0
select b;

The important change is begin_prefix <= b.Starts instead of begin_prefix >= b.Starts. I also added a check that bEnds >= b.Starts, just to be sure.

BTW: The part you marked as IMPORTANT seems like complete nonsense to me.

我的黑色迷你裙 2024-10-28 03:18:09

你的 begin_prefix <= b.starts 和 > 不应该吗?结束前缀?

Shouldn't your begin_prefix <= b.starts and > end_prefix?

思念满溢 2024-10-28 03:18:09

通过这种方式,您将在以下范围内选择正确的预订:

IQueryable<booking> bQ = from b in db.bookings
                         where b.Starts >= begin_prefix && b.Ends <= end_prefix
                         where b.pointsbookings.Any()
                         select b;

In this way you will select the correct bookings in the range:

IQueryable<booking> bQ = from b in db.bookings
                         where b.Starts >= begin_prefix && b.Ends <= end_prefix
                         where b.pointsbookings.Any()
                         select b;
孤独难免 2024-10-28 03:18:09

此处:

IQueryable<booking> bQ = from b in db.bookings
                         where b.Starts >= begin_prefix &&
                               b.Ends <= end_prefix &&
                               b.Starts < b.Ends
                               b.pointsbookings.Count > 0
                         select b;

您希望开始时间位于开始前缀 (b.Starts >= begin_prefix) 之后,结束时间位于结束前缀之前 (b.Ends <= end_prefix),以及开始之后的结束(b.Starts > b.Ends)。这就是这段代码现在所说的内容。你基本上扭转了不平等。

Here:

IQueryable<booking> bQ = from b in db.bookings
                         where b.Starts >= begin_prefix &&
                               b.Ends <= end_prefix &&
                               b.Starts < b.Ends
                               b.pointsbookings.Count > 0
                         select b;

You want the start time to be after the begin prefix (b.Starts >= begin_prefix), the end time to be before the end prefix (b.Ends <= end_prefix), and the end to come after the start (b.Starts > b.Ends). That is what this code is now saying. You basically had the inequalities reversed.

多像笑话 2024-10-28 03:18:09

不是 100% 确定这些代表什么,但我想知道为什么 begin_prefix >= b.Starts;不应该是相反的吗?

Not 100% sure what these represent but I'm wondering why begin_prefix >= b.Starts; shouldn't it be the reverse?

尬尬 2024-10-28 03:18:09

我认为这个问题的意思是,“从日期时间范围列表中,我如何找到与给定日期时间范围重叠的所有内容?”

如果这确实是您所要求的,那么您的查询正在寻找的是

//b overlaps the datetime-range [begin_prefix, end_prefix]
begin_prefix <= b.Ends && end_prefix >= b.Starts

I took this question to mean, "From a list of datetime-ranges, how can I find all that overlap a given datetime-range?"

If that is truly what you are asking, then the query you are looking for is

//b overlaps the datetime-range [begin_prefix, end_prefix]
begin_prefix <= b.Ends && end_prefix >= b.Starts
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文