Linq-获取具有开始和长度属性的日历事件

Linq-获取具有开始和长度属性的日历事件,linq,entity-framework,calendar,Linq,Entity Framework,Calendar,我的应用程序有一个内置日历系统,它们的数据库模式如下所示: CalendarItem( CalendarItemId bigint, Start datetime, Length int, Blargh nvarchar(MAX) ) Start是事件开始时的UTC日期时间值,Length是以秒为单位的事件长度。全天活动从0000h开始,长度为86400 我将Linq与实体框架结合使用,我希望找到日期范围内的事件。很容易找到在两个日期时间之间开始的事件,但我不知道如何找到在两个日期时间之间也结

我的应用程序有一个内置日历系统,它们的数据库模式如下所示:

CalendarItem( CalendarItemId bigint, Start datetime, Length int, Blargh nvarchar(MAX) )
Start
是事件开始时的UTC日期时间值,
Length
是以秒为单位的事件长度。全天活动从0000h开始,长度为86400

我将Linq与实体框架结合使用,我希望找到日期范围内的事件。很容易找到在两个日期时间之间开始的事件,但我不知道如何找到在两个日期时间之间也结束的事件

以下是我当前的代码:

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;
}
public IEnumerable GetCalendarItems(日期时间从,日期时间到){
var events=来自db.CalendarItems中的c

其中c.Start>=from&&c.Start您需要调用
ToList()
首先,您可以使用
DateTime.AddSeconds
函数。否则,编译器会抱怨它找不到
AddSeconds
函数,因为您的LINQ查询将被转换为SQL,并且SQL不包含此
DateTime.AddSeconds
函数

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;
var events=(来自db.CalendarItems中的c

其中c.Start>=from&&c.Start e.Start.AddSeconds(Length)您需要调用
ToList()
首先,您可以使用
DateTime.AddSeconds
函数。否则,编译器会抱怨它找不到
AddSeconds
函数,因为您的LINQ查询将被转换为SQL,并且SQL不包含此
DateTime.AddSeconds
函数

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;
var events=(来自db.CalendarItems中的c
其中c.Start>=from&&c.Start e.Start.AddSeconds(长度)使用包含的ToList()函数编辑:

如果我读对了,你会想:

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;
var events=(来自db.CalendarItems中的c
其中c.Start>=from&&c.Start e.Start.AddSeconds(长度)使用包含的ToList()函数编辑:

如果我读对了,你会想:

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;
var events=(来自db.CalendarItems中的c

其中c.Start>=from&&c.Start e.Start.AddSeconds(Length)我评估了
.ToList
方法,但它们效率低下,因为在修改它以返回在一个时间段内发生的事件(无论它们是否开始或结束)后,它会从数据库中获取许多不相关的结果

我还研究了SqlFunctions方法,但它们在EF1.0中不存在

最后,我在实体上下文中使用了一个带有强类型导入的存储过程。这并不完美,但比其他方法要好


当项目最终升级到.NET4时,我将切换到SqlFunctions。无论如何,感谢所有的建议!

我评估了
.ToList
方法,但它们效率低下,因为在修改它以返回发生的事件(不管它们是否在一个时间段,它从数据库中获取许多不相关的结果

我还研究了SqlFunctions方法,但它们在EF1.0中不存在

最后,我在实体上下文中使用了一个带有强类型导入的存储过程。这并不完美,但比其他方法要好


当项目最终升级到.NET4时,我将切换到SqlFunctions。无论如何,感谢所有的建议!

请参阅查看您的事件。Where子句将始终返回所有内容。e.Start始终小于e.Start.ADDSECONS(长度)。请查看我的答案以获得更好的解决方案。@IronMan84谢谢您的回答。谢谢您将我的答案整合为一个。您的events.Where子句将始终返回所有内容。e.Start始终小于e.Start.AddSeconds(长度)。请查看我的答案以获得更好的解决方案。@IronMan84谢谢您的回答。谢谢您将我的答案整合为一个答案。