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

查看:62
本文介绍了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);

<小时>

错误:

base {System.SystemException} = {"指定的类型成员 'Date' 是LINQ to Entities 不支持.只有初始值设定项、实体成员、并且支持实体导航属性."}

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天全站免登陆