gpt4 book ai didi

entity-framework-4 - Linq 中的 DateTime 操作到实体查询

转载 作者:行者123 更新时间:2023-12-04 05:34:28 24 4
gpt4 key购买 nike

运行此代码:

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 是整数。我不确定是什么原因造成的:
AssignmentDateTime 是 DateTime?也许这就是问题所在?

最佳答案

使用 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;
}
}

请注意 assignments永远不会为空(但它可能为空——测试 assignments.Any())。

关于entity-framework-4 - Linq 中的 DateTime 操作到实体查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4832523/

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