【问题标题】:LINQ - using a query expression to calculate data time differenceLINQ - 使用查询表达式计算数据时间差
【发布时间】:2011-07-08 10:31:07
【问题描述】:

我使用 EntityFramework 4、LINQ 和 C#。

我需要使用 Linq 在 GridView 中显示来自查询表达式的数据;我需要将项目作为anonymous type 即时计算DateTime.UtcNow - cl.DateLocked 注意:cl.DateLocked 的类型为DateTime,我需要知道这两个日期之间相差多少天。

使用这个查询我收到一个错误:

DbArithmeticExpression arguments must have a numeric common type. 

知道如何在 Linq 中做到这一点吗?如果 Linq 不允许它如何以不同的方式做到这一点?

感谢您的宝贵时间

var queryContentsLocked = from cl in context.CmsContentsLockedBies
                          join cnt in context.CmsContents
                          on cl.ContentId equals cnt.ContentId
                          join u in context.aspnet_Users
                          on cl.UserId equals u.UserId
                          select new
                          {
                            cl.DateLocked,
                            cnt.ContentId,
                            cnt.Title,
                            u.UserName,
                            u.UserId,
                            TimePan = DateTime.UtcNow - cl.DateLocked // Problem here!!!
                          };

【问题讨论】:

    标签: linq datetime linq-to-entities


    【解决方案1】:
    var queryContentsLocked = from cl in context.CmsContentsLockedBies
                              join cnt in context.CmsContents
                              on cl.ContentId equals cnt.ContentId
                              join u in context.aspnet_Users
                              on cl.UserId equals u.UserId
                              select new
                              {
                                cl.DateLocked,
                                cnt.ContentId,
                                cnt.Title,
                                u.UserName,
                                u.UserId,
                                Days = SqlFunctions.DateDiff("day", cl.DateLocked, DateTime.UtcNow)
                              };
    

    【讨论】:

    • 感谢@Magnus,它运行良好。 DateDiff 方法在命名空间中: using System.Data.Objects.SqlClient;
    【解决方案2】:

    不使用任何SqlFunction

    var grandTotalWork = (from t in db.AssignmentHandymandetails
                               join ad in db.AssignmentDetails on t.assignment_detail_id equals ad.assignment_detail_id
                               where ad.assignment_id == Convert.ToInt32(Label_assignmentId.Text)
                                select new { startTime = t.started_time.Value.TimeOfDay, endTime = t.end_time.Value.TimeOfDay, TotalWorkTime = (t.started_time.Value.TimeOfDay.Duration() - t.end_time.Value.TimeOfDay.Duration()).Duration()});
    

    然后你可以将结果绑定到你想要的GridView中

    【讨论】:

    • 感谢您的建议,现在代码可读了,我非常感谢它
    【解决方案3】:

    试试这个:

    DateTime.UtcNow.Subtract(cl.DateLocked).TotalDays
    

    【讨论】:

    • 错误:无效的匿名类型成员声明器。它应该在 Linq 查询表达式中完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多