gpt4 book ai didi

linq - LINQ to Entities 不支持指定的类型成员 'Date'。仅初始值设定项、实体成员和实体导航属性

转载 作者:行者123 更新时间:2023-12-03 04:32:19 25 4
gpt4 key购买 nike

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

代码:

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

错误:

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获取日期部分的方法。

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

更新:正如 @shankbond 在评论中提到的,在 Entity Framework 6 EntityFunctions 中已过时,您应该使用 DbFunctions类,它随 Entity Framework 一起提供。

关于linq - LINQ to Entities 不支持指定的类型成员 'Date'。仅初始值设定项、实体成员和实体导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14601676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com