我如何修改此查询以获得我想要的 - 日历日期

发布于 2024-08-16 08:52:55 字数 667 浏览 4 评论 0原文

我有这个查询

return plannerDb.IndividualCalendars.Where(u => 
    u.UserId == userId && 
    u.StartDate.Date >= startDate && 
    u.EndDate.Date <= endDate)

,所以基本上这个查询的作用是查找从该月初到该月底的所有日历事件。

然而,这并没有考虑到这样的事件,

Start Date: 12/26/2009 12:00:00 AM
End Date: 1/8/2010 12:00:00 AM

所以今天是 12 月 30 日。我的日历显示从 11 月 29 日到 1 月 8 日。

所以这个活动将在本月展示。然而,下个月一月到来时,它将显示从12月27日到2月6日。

现在,由于开始日期不在这个范围内,它将忽略此事件并且不显示它。

所以我需要一种方法来展示这些类型的事件,但同时只收集需要的事件。

我总是可以从用户那里获取每个事件,但我认为这太浪费了,因为我必须循环遍历并对其进行排序。

所以我只想显示将要显示的记录。

正如你所看到的,我正在使用 linq。

谢谢

I have this query

return plannerDb.IndividualCalendars.Where(u => 
    u.UserId == userId && 
    u.StartDate.Date >= startDate && 
    u.EndDate.Date <= endDate)

so basically what this query does is that is looks for all calendar events that range from the start of that month to the end of that month.

However this does not account for events such as this

Start Date: 12/26/2009 12:00:00 AM
End Date: 1/8/2010 12:00:00 AM

So today is December 30th. My calendar displays from Nov 29th to Jan 8th.

So this event will be shown this month. However next month when January comes around it will show from December 27th to Feb 6th.

Now since the start date is not in this range it will ignore this event and not display it.

So I need a way to show these kinds of events but at the same time only gather the ones that are needed.

I could always just get every event from the user they had but I think that is such a waste since I am going to have to loop through that and sort it.

So I want only records that will be shown.

So as you can see I am using linq.

Thanks

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

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

发布评论

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

评论(3

你与清晨阳光 2024-08-23 08:52:55

如果我正确理解您的要求,您只需要与间隔 [startdate, endDate] 重叠的事件。如果这是正确的,这将为您完成:

plannerDb.IndividualCalendars.Where(
    u => u.UserId == userId &&
        (u.StartDate.Date < startDate && (
            (u.StartDate.Date >= startDate && u.StartDate.Date <= startDate) || 
            (u.EndDate.Date >= endDate && u.EndDate.Date <= endDate) ||
            (u.StartDate.Date <= startDate && u.EndDate.Date >= endDate)
        )
);

基本上,您只需要知道 u.StartDate.Date 是否在 [startDate, endDate]u 中.EndDate.Date 位于 [startDate, endDate] 中或者间隔 [u.StartDate.Date, u.EndDate.Date] 包含 [开始日期,结束日期]。也许更简单的表达方式是:

plannerDb.IndividualCalendars.Where(
    u => u.UserId == userId &&
        !(u.StartDate.Date > endDate || u.EndDate.Date < startDate)
);

即事件不能在 endDate 之后开始或在 startDate 之前结束。同样:

plannerDb.IndividualCalendars.Where(
    u => u.UserId == userId &&
        (u.StartDate.Date <= endDate && u.EndDate.Date >= startDate)
);

最后一个版本是最清晰的。它表示事件必须在 endDate 之前开始并在 startDate 之后结束。

If I understand your requirements correctly, you only need events that overlap the interval [startdate, endDate]. If that's correct, this will do it for you:

plannerDb.IndividualCalendars.Where(
    u => u.UserId == userId &&
        (u.StartDate.Date < startDate && (
            (u.StartDate.Date >= startDate && u.StartDate.Date <= startDate) || 
            (u.EndDate.Date >= endDate && u.EndDate.Date <= endDate) ||
            (u.StartDate.Date <= startDate && u.EndDate.Date >= endDate)
        )
);

Basically, you only need to know if u.StartDate.Date is in [startDate, endDate] or u.EndDate.Date is in [startDate, endDate] or if the interval [u.StartDate.Date, u.EndDate.Date] contains [startDate, endDate]. Perhaps a simpler way to express this is:

plannerDb.IndividualCalendars.Where(
    u => u.UserId == userId &&
        !(u.StartDate.Date > endDate || u.EndDate.Date < startDate)
);

That is, the event can not start after the endDate or end before the startDate. Equivalently:

plannerDb.IndividualCalendars.Where(
    u => u.UserId == userId &&
        (u.StartDate.Date <= endDate && u.EndDate.Date >= startDate)
);

This last version is the clearest. It says that the event has to start before the endDate and end after the startDate.

橪书 2024-08-23 08:52:55

听起来你只是想稍微改变一下查询,

return plannerDb.IndividualCalendars.Where(u => u.UserId == userId
   && (
        (startDate <= u.StartDate.Date && endDate >= u.StartDate)  // Is start date in range
     || (startDate <= u.EndDate.Date && endDate >= u.EndDate)  // Is end date in range
     || (startDate > u.StartDate.Date && endDate < u.EndDate)  // Is range within start/end
      )
   )

我认为这会解决它,除非我犯了一个小错误。

Sounds like you just want to change the query a bit

return plannerDb.IndividualCalendars.Where(u => u.UserId == userId
   && (
        (startDate <= u.StartDate.Date && endDate >= u.StartDate)  // Is start date in range
     || (startDate <= u.EndDate.Date && endDate >= u.EndDate)  // Is end date in range
     || (startDate > u.StartDate.Date && endDate < u.EndDate)  // Is range within start/end
      )
   )

I think that will solve it, unless I've made a trivial mistake.

自此以后,行同陌路 2024-08-23 08:52:55

返回 plannerDb.IndividualCalendars.Where(u => u.UserId == userId && u.StartDate.Date <= endDate && (u.StartDate.Date >= startDate || u.EndDate.日期 >= 开始日期)

return plannerDb.IndividualCalendars.Where(u => u.UserId == userId && u.StartDate.Date <= endDate && (u.StartDate.Date >= startDate || u.EndDate.Date >= startDate)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文