我试图通过这个linq查询获得10天前的记录。 [英] I am trying to get record 10 day before by this linq query.
本文介绍了我试图通过这个linq查询获得10天前的记录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
其他信息:LINQ to Entities无法识别方法'System.DateTime AddDays(Double)'方法,并且此方法无法转换为商店表达式。
我的尝试:
DateTime dtNow = DateTime.Now;
var lst = db.SMSHistory.Where(s => s.UserID == IUserID&& EntityFunctions.TruncateTime(s.SendDate)== EntityFunctions.TruncateTime(dtNow.Date.AddDays(-10) ))。选择(d => d)。ToList()
解决方案
错误信息非常清楚。作为一个消息,实体框架无法将DateTime.AddDays(n)
转换为SQL表达式。您必须使用 SqlFunctions [ ^ ],尤指: SqlFunctions.DateAdd Method( String,Nullable(Double),Nullable(DateTime))(System.Data.Objects.SqlClient) [ ^ ]。或者......您可以使用 F-ES Sitecore [ ^ ]在问题的评论中。
检查我过去对类似问题的回答: LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.Object)'方法,并且此方法无法转换为商店表达式。 [ ^ ]
详情请见:
c# - 方法无法转换为商店表达式 - Stack溢出 [ ^ ]
Additional information: LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.
What I have tried:
DateTime dtNow = DateTime.Now; var lst = db.SMSHistory.Where(s => s.UserID == IUserID && EntityFunctions.TruncateTime(s.SendDate) == EntityFunctions.TruncateTime(dtNow.Date.AddDays(-10))).Select(d=>d).ToList()
解决方案
The error message is quite clear. As a messege states Entity Framework cannot convertDateTime.AddDays(n)
into SQL expression. You have to use SqlFunctions[^], especially: SqlFunctions.DateAdd Method (String, Nullable(Double), Nullable(DateTime)) (System.Data.Objects.SqlClient)[^]. Or... you can use method provided by F-ES Sitecore[^] in the comment to the question.
Check my past answer to similar question: LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.Object)' method, and this method cannot be translated into a store expression.[^]
For further details, please see:
c# - Method cannot be translated into a store expression - Stack Overflow[^]
这篇关于我试图通过这个linq查询获得10天前的记录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文