Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架Linq查询中的日期比较_C#_Entity Framework - Fatal编程技术网

C# 实体框架Linq查询中的日期比较

C# 实体框架Linq查询中的日期比较,c#,entity-framework,C#,Entity Framework,我正在尝试比较EntityFramework6LINQ查询中的日期 var bookings = context.Bookings.Include("BookingItems").Where(x=>x.IsDeleted == false && DateTime.Compare(DbFunctions.TruncateTime(x.BookingDate).Value,DateTime.Now.Date)==0).ToList(); 但是,它会导致例外情况: "The s

我正在尝试比较EntityFramework6LINQ查询中的日期

var bookings = context.Bookings.Include("BookingItems").Where(x=>x.IsDeleted == false && DateTime.Compare(DbFunctions.TruncateTime(x.BookingDate).Value,DateTime.Now.Date)==0).ToList();
但是,它会导致例外情况:

"The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
我只想通过调整时间部分来比较日期。


有什么建议可以这样做吗?

Linq to sql或sql没有implemination DateTime。比较

尝试转换到集合

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

Linq无法将某些本机.Net代码转换为Sql,因此请尝试使用DateTime.Now函数和Linq查询之外的其他.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();

虽然可能有一些语法错误,但我还没有运行过,但还是要尝试一下这个异常:{“这个函数只能从LINQ调用到实体。”}你能举一个例子说明如何在查询中引用它吗这是一个技巧DateTime temp=DateTime.now.Date;var bookings=context.bookings.Include(“bookingtimes”)。其中(x=>x.IsDeleted==false&&DateTime.Compare(DbFunctions.TruncateTime(x.BookingDate.Value,temp)==0.ToList();