gpt4 book ai didi

asp.net - Entity Framework 和 Linq - 比较 DateTime

转载 作者:行者123 更新时间:2023-12-02 13:47:19 30 4
gpt4 key购买 nike

我有这个代码

public List<CalendarData> GetCalendarData(DateTime day)
{
List<CalendarData> list = new List<CalendarData>();
using (dataContext = new VTCEntities())
{

DateTime test = new DateTime(2010, 10, 20, 17, 45, 0);

var data = from z in dataContext.ReservationsSet
where z.start_time.Value == test
select z;

foreach (var r in data)

我想做的就是拥有这个

var data = from z in dataContext.ReservationsSet
where z.start_time.Value == day
select z;

我遇到的问题是 z.start_time 也有时间部分。 DateTime 日没有记录时间部分。有没有办法比较日期部分而不会出现此错误

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

当我这样做时

var data = from z in dataContext.ReservationsSet
where z.start_time.Value.Date == test
select z;

最佳答案

一种选择是计算两个值,如下所示:

DateTime day = ...;
DateTime nextDay = day.AddDays(1);

var data = from z in dataContext.ReservationsSet
where z.start_time.Value >= day &&
z.start_time.Value < nextDay
select z;

关于asp.net - Entity Framework 和 Linq - 比较 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3970737/

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