Linq toEntity 查询中的日期时间操作

发布于 2024-10-15 08:25:48 字数 836 浏览 0 评论 0原文

运行此代码:

public bool CheckTime(DateTime date, int UserID, int TotalTimeMin)
        {
            using (var context = new myDB())
            {         
                var assginments = from c in context.Assignments.Where(x=>(x.AssignmentDateTime < date && x.AssignmentDateTime.Value.AddMinutes(TotalTimeMin) > date) || 
                    (x.AssignmentDateTime < date.AddMinutes(TotalTimeMin))) select c;

                if(assginments != null) return false;
                else return true;
            }


        }

我收到此错误。

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

TotalTimeMin 是 int。我不确定是什么原因造成的: 分配日期时间是日期时间吗?也许这就是问题所在?

running this code:

public bool CheckTime(DateTime date, int UserID, int TotalTimeMin)
        {
            using (var context = new myDB())
            {         
                var assginments = from c in context.Assignments.Where(x=>(x.AssignmentDateTime < date && x.AssignmentDateTime.Value.AddMinutes(TotalTimeMin) > date) || 
                    (x.AssignmentDateTime < date.AddMinutes(TotalTimeMin))) select c;

                if(assginments != null) return false;
                else return true;
            }


        }

I get this error.

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

TotalTimeMin is int. I am not sure what cause this:
AssignmentDateTime is DateTime? and maybe this is the problem ?

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

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

发布评论

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

评论(1

超可爱的懒熊 2024-10-22 08:25:48

使用 EntityFunctions.AddMinutes(需要 EF 4):

    public bool CheckTime(DateTime date, int UserID, int TotalTimeMin)
    {
        using (var context = new myDB())
        {         
            var assginments = context.Assignments
                                     .Where(x=>(x.AssignmentDateTime < date 
                                                && EntityFunctions.AddMinutes(x.AssignmentDateTime,TotalTimeMin) > date) 
                                                || (x.AssignmentDateTime < date.AddMinutes(TotalTimeMin)));

            if(assginments != null) return false;
            else return true;
        }
    }

请注意,分配将永远为空(但它可能为空 - 测试 作业.Any())。

Use EntityFunctions.AddMinutes (requires EF 4):

    public bool CheckTime(DateTime date, int UserID, int TotalTimeMin)
    {
        using (var context = new myDB())
        {         
            var assginments = context.Assignments
                                     .Where(x=>(x.AssignmentDateTime < date 
                                                && EntityFunctions.AddMinutes(x.AssignmentDateTime,TotalTimeMin) > date) 
                                                || (x.AssignmentDateTime < date.AddMinutes(TotalTimeMin)));

            if(assginments != null) return false;
            else return true;
        }
    }

Note that assignments will never be null (but it might be empty -- test assignments.Any()).

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