实体框架Linq查询中的日期比较 [英] Date Comparison In Entity Framework Linq Query

查看:99
本文介绍了实体框架Linq查询中的日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较Entity Framework 6 LINQ查询中的日期。

  var bookings = context.Bookings.Include( BookingItems)。其中(x => x.IsDeleted == false& DateTime.Compare(DbFunctions.TruncateTime(x.BookingDate).Value,DateTime.Now.Date)== 0).ToList(); 

但是会导致异常:



<$ p $ p> LINQ to Entities不支持指定的类型成员'Date'。仅支持初始化程序,实体成员和实体导航属性。

我只想通过减少时间部分来比较日期。



有什么建议可以做到吗?

解决方案

Linq无法翻译一些本地.Net代码进入Sql,因此尝试在Linq查询之外使用DateTime.Now函数和其他.Net函数,然后在查询中引用它。

  String currentTime = DateTime.Now.ToShortDateString(); 

var bookings = context.Bookings.Include( BookingItems)。Where(x => x.IsDeleted == false&& DateTime.Compare((x.BookingDate).Value。)。 Date,currentTime)== 0).ToList();

虽然可能存在语法错误,但我还没有运行,但是还是尝试


I am trying to compare dates in Entity Framework 6 LINQ Query.

var bookings = context.Bookings.Include("BookingItems").Where(x=>x.IsDeleted == false && DateTime.Compare(DbFunctions.TruncateTime(x.BookingDate).Value,DateTime.Now.Date)==0).ToList();

However its resulting in Exception:

"The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."

I just want to compare dates by trimming time part.

Any suggestions how this can be done?

解决方案

Linq can't translate some native .Net code into Sql so try to use DateTime.Now function and other .Net function outside Linq query and then reference it in query.

String currentTime = DateTime.Now.ToShortDateString();

var bookings = context.Bookings.Include("BookingItems").Where(x=>x.IsDeleted == false && DateTime.Compare((x.BookingDate).Value.Date,currentTime)==0).ToList();

I haven't run though there might be some syntax error, but try anyway

这篇关于实体框架Linq查询中的日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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