Linq - 获取具有Start和Length属性的日历事件 [英] Linq - Getting calendar events that have Start and Length properties

查看:165
本文介绍了Linq - 获取具有Start和Length属性的日历事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式有内建的日历系统,其数据库架构如下:

My application has a built-in calendaring system and the database schema for them looks like this:

CalendarItem( CalendarItemId bigint, Start datetime, Length int, Blargh nvarchar(MAX) )

/ code>是事件开始时的UTC datetime值, Length 是事件的长度(以秒为单位)。一个全天的活动开始时间为0000h,长度为86400.

Start is the UTC datetime value when the event starts, and Length is the length of the event in seconds. An all-day event starts at 0000h and has a length of 86400.

我使用Linq与Entity Framework,我想查找属于某个日期的事件范围。很容易找到在两个数据时间之间开始的事件,但我不知道如何在两个数据时间之间找到结束的事件。

I'm using Linq with Entity Framework, and I want to find events that fall within a date range. It's easy to find events that start between two datetimes, but I don't know how to find events that also end between two datetimes.

这是我当前的代码:

public IEnumerable<CalendarItem> GetCalendarItems(DateTime from, DateTime to) {

    var events = from c in db.CalendarItems
                 where c.Start >= from && c.Start <= to
                 orderby c.Start
                 select c;

    return events;
}



如果我使用T-SQL,我需要使用 DATEADD 添加长度秒到开始给出 End datetime,这将工作,但我不认为我可以在Linq做到这一点。我可以做什么?

If I were using T-SQL, I would need to use DATEADD to add Length seconds to Start to give an End datetime, which would then work, but I don't think I can do this in Linq. What can I do?

推荐答案

已使用ToList()函数修改:

EDITED with ToList() function included:

如果我正确读取,您将需要:

If I'm reading this correctly, you would want:

var events = (from c in db.CalendarItems
             where c.Start >= from && c.Start <= to
             orderby c.Start
             select c).ToList();
events = events.Where(e => e.Start.AddSeconds(Length) <= to);

return events;

然后会显示在指定日期范围内开始和结束的活动。

This will then give you the events that started and ended during the specified date range.

有关DateTime.AddSeconds()的更多信息,只需访问此链接

For more information about DateTime.AddSeconds(), just visit this link.

这篇关于Linq - 获取具有Start和Length属性的日历事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆