在 SQL CE 数据库上的实体框架中使用 DateDiff

发布于 2024-10-09 18:32:19 字数 1407 浏览 3 评论 0原文

我有一个方法应该返回带有计算列的匿名对象列表,如下所示:

        var tomorrow = DateTime.Today.AddDays(1);
        return from t in this.Events
                where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
                select new
                {
                    Client = t.Activity.Project.Customer.Name,
                    Project = t.Activity.Project.Name,
                    Task = t.Activity.Task.Name,
                    Rate = t.Activity.Rate.Name,
                    StartTime = t.StartTime,
                    EndTime = t.EndTime.Value,
                    Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("m", t.StartTime, t.EndTime.Value) / 60),
                    Description = t.Activity.Description
                };

不幸的是,我从 DateDiff 函数中收到以下错误:

指定的方法 'System.Nullable1[System.Int32] DateDiff类型“System.Data.Objects.SqlClient.SqlFunctions”上的 (System.String, System.Nullable1[System.DateTime], System.Nullable`1[System.DateTime])' 无法转换为LINQ to Entities 存储表达式。

有什么想法我可能在这里做错了吗?

编辑:我还尝试了这里提到的EntityFunctions类,但这并没有那么有效。

Minutes = EntityFunctions.DiffMinutes(t.EndTime, t.StartTime),

I have a method which should return a list of anonymous objects with a calculated column like this:

        var tomorrow = DateTime.Today.AddDays(1);
        return from t in this.Events
                where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
                select new
                {
                    Client = t.Activity.Project.Customer.Name,
                    Project = t.Activity.Project.Name,
                    Task = t.Activity.Task.Name,
                    Rate = t.Activity.Rate.Name,
                    StartTime = t.StartTime,
                    EndTime = t.EndTime.Value,
                    Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("m", t.StartTime, t.EndTime.Value) / 60),
                    Description = t.Activity.Description
                };

Unfortunately I get the following error from the DateDiff function:

The specified method 'System.Nullable1[System.Int32] DateDiff(System.String, System.Nullable1[System.DateTime], System.Nullable`1[System.DateTime])' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression.

Any ideas what I could have done wrong here?

EDIT: I also tried the EntityFunctions class mentioned here, but that did not work as well.

Minutes = EntityFunctions.DiffMinutes(t.EndTime, t.StartTime),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单 2024-10-16 18:32:19

[编辑]

Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("mi", t.StartTime, t.EndTime.Value) / 60)

不支持 SQL CE。

Hours = ((TimeSpan)(t.EndTime.Value - t.StartTime)).TotalHours

引发 DbArithmeticExpression 异常

因此,我认为您必须分两步完成。抓取所需的数据,然后计算内存中的时间差。

var events = (from t in context.Events
    where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
    select t).ToArray();

return from t in events
    select new
    {
         ...
         Hours = (t.EndTime.Value - t.StartTime).TotalHours
    };

[Edit]

Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("mi", t.StartTime, t.EndTime.Value) / 60)

is not supported SQL CE.

Hours = ((TimeSpan)(t.EndTime.Value - t.StartTime)).TotalHours

Throws an DbArithmeticExpression Exception

So, I think you'll have to do it in two steps. Grab the data you need, then calculate the time difference in memory.

var events = (from t in context.Events
    where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
    select t).ToArray();

return from t in events
    select new
    {
         ...
         Hours = (t.EndTime.Value - t.StartTime).TotalHours
    };
谈场末日恋爱 2024-10-16 18:32:19

您要求 EF 提供程序处理 SQL 中的 DATEDIFF,我认为这在任何版本的 SQL 中都是不可能的。使用本机日期时间函数:

var tomorrow = DateTime.Today.AddDays(1);
        return from t in this.Events
                where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
                select new
                {
                    Client = t.Activity.Project.Customer.Name,
                    Project = t.Activity.Project.Name,
                    Task = t.Activity.Task.Name,
                    Rate = t.Activity.Rate.Name,
                    StartTime = t.StartTime,
                    EndTime = t.EndTime.Value,
                    Hours = t.EndTime.Value(t.StartTime.Subtract).Hours,
                    Description = t.Activity.Description
                };

You're asking the EF provider to handle the DATEDIFF in SQL, which I don't think is possible in any version of SQL. Use native DateTime functions:

var tomorrow = DateTime.Today.AddDays(1);
        return from t in this.Events
                where (t.StartTime >= DateTime.Today && t.StartTime < tomorrow && t.EndTime.HasValue)
                select new
                {
                    Client = t.Activity.Project.Customer.Name,
                    Project = t.Activity.Project.Name,
                    Task = t.Activity.Task.Name,
                    Rate = t.Activity.Rate.Name,
                    StartTime = t.StartTime,
                    EndTime = t.EndTime.Value,
                    Hours = t.EndTime.Value(t.StartTime.Subtract).Hours,
                    Description = t.Activity.Description
                };
时光磨忆 2024-10-16 18:32:19

两种可能性,从 EndTime 中删除 Value:

Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("m", t.StartTime, t.EndTime) / 60) 

或使用 DateDiffHour:

Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiffHour(t.StartTime, t.EndTime)

Two possibilities, remove Value from EndTime:

Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiff("m", t.StartTime, t.EndTime) / 60) 

Or use DateDiffHour:

Hours = (System.Data.Objects.SqlClient.SqlFunctions.DateDiffHour(t.StartTime, t.EndTime)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文