gpt4 book ai didi

c# - 计算 Entity Framework 中日期时间对象的时差给出错误

转载 作者:行者123 更新时间:2023-11-30 23:23:44 25 4
gpt4 key购买 nike

<分区>

我正在尝试计算 2 个日期之间的时差,并想获取我约会的日期名称。

例如:2016 年 4 月 7 日

  • 工作日:星期一

这是我的类(class):

public class Attendance
{
public int Id { get; set; }
public Nullable<System.DateTime> StartDateTime { get; set; }
public Nullable<System.DateTime> EndDateTime { get; set; }
}

当我尝试这样做时:

 var query = (from t in context.Attendance
select new
{
timeDiff=t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalHours,
Day=System.StartDateTime.ToString("dddd");
}).tolist();

错误

LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method, and this method cannot be translated into a store expression

我不想像下面那样做:

 var query = (from t in context.Attendance.toList().
select new
{
timeDiff=t.EndDateTime.Value.Subtract(t.StartDateTime.Value).TotalHours,
Day=System.StartDateTime.ToString("dddd");
}).tolist();

我的表中存储了这样格式的 Datetime,所以我希望在差异字段中显示如下所示的预期输出:

startDatetime              Enddatetime                Difference
----------------------------------------------------------------
2016-06-29 15:52:32.360 2016-06-29 15:52:36.970 00:00:04
2016-06-29 15:53:32.360 2016-06-29 15:55:36.970 00:2:00
2016-06-29 15:53:32.360 2016-06-29 16:55:36.970 01:02:00

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