我如何修改此查询以获得我想要的 - 日历日期
我有这个查询
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解您的要求,您只需要与间隔
[startdate, endDate
] 重叠的事件。如果这是正确的,这将为您完成:基本上,您只需要知道
u.StartDate.Date
是否在[startDate, endDate]
或u 中.EndDate.Date
位于[startDate, endDate]
中或者间隔[u.StartDate.Date, u.EndDate.Date]
包含[开始日期,结束日期]
。也许更简单的表达方式是:即事件不能在
endDate
之后开始或在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:Basically, you only need to know if
u.StartDate.Date
is in[startDate, endDate]
oru.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:That is, the event can not start after the
endDate
or end before thestartDate
. Equivalently:This last version is the clearest. It says that the event has to start before the
endDate
and end after thestartDate
.听起来你只是想稍微改变一下查询,
我认为这会解决它,除非我犯了一个小错误。
Sounds like you just want to change the query a bit
I think that will solve it, unless I've made a trivial mistake.
返回 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)