我正在尝试删除表格中超过3小时的所有项目,但会收到以下错误...
DbArithmeticExpression参数必须具有数字公共类型。
// Method to clean items in baskets table which are over 3 hours old.
public void CleanBasket()
{
var expired = (from a in db.Baskets
where (DateTime.Now - a.DateCreated).TotalHours > 3 select a);
foreach (Basket basket in expired) db.DeleteObject(expired);
db.SaveChanges();
}
我之前从未见过这个错误,有人可以帮我调试吗?
仅供参考,我也试过...... var expired =(来自db.Baskets中的一个(DateTime.Now.Subtract(a.DateCreated).Hours> 3)选择a);
但是我收到错误消息“LINQ to Entities无法识别方法'System.TimeSpan Subtract(System.DateTime)'方法,并且此方法无法转换为商店表达式。”
答案 0 :(得分:2)
EF不支持DateTime - DateTime
。
答案 1 :(得分:0)
您需要根据以下答案使用System.Data.Entity.DbFunctions.DiffHours:
https://stackoverflow.com/a/9860858/345659
var expired = (from a in db.Baskets
where DbFunctions.DiffHours(DateTime.Now, a.DateCreated) > 3
select a);