LINQ to Entities不支持指定的类型成员'Date'。只有初始化器,实体成员和实体导航属性 [英] The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties

查看:718
本文介绍了LINQ to Entities不支持指定的类型成员'Date'。只有初始化器,实体成员和实体导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架中使用此代码,我收到以下错误。我需要获取特定日期的所有行, DateTimeStart 的格式为DataType,格式为 2013-01-30 12:00:00.000

Using this code in Entity Framework I receive the following error. I need to get all the rows for a specific date, DateTimeStart is of type DataType in this format 2013-01-30 12:00:00.000

代码:

 var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
                    .Where(x =>  x.DateTimeStart.Date == currentDateTime.Date);






错误:


Error:

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

任何想法如何解决?

推荐答案

DateTime.Date 无法转换为SQL。使用 EntityFunctions.TruncateTime 方法获取日期部分。

DateTime.Date cannot be converted to SQL. Use EntityFunctions.TruncateTime method to get date part.

var eventsCustom = eventCustomRepository
.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
.Where(x => EntityFunctions.TruncateTime(x.DateTimeStart) == currentDate.Date);

更新:作为@shankbond在注释中提到的实体框架6 EntityFunctions 已过时,您应该使用 DbFunctions 类,这是实体框架附带的。

UPDATE: As @shankbond mentioned in comments, in Entity Framework 6 EntityFunctions is obsolete, and you should use DbFunctions class, which is shipped with Entity Framework.

这篇关于LINQ to Entities不支持指定的类型成员'Date'。只有初始化器,实体成员和实体导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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