LINQ to Entities无法识别方法'System.DateTime Parse(System.String)'方法,并且此方法无法转换为存储表达式。 [英] LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method, and this method cannot be translated into a store expression.
问题描述
嗨
我得到一个错误说:
LINQ to Entities无法识别方法'System.DateTime Parse(System。 String)'方法,这个方法无法翻译成商店表达式。
这是我各自的代码
Hi
im getting an error saying :
LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method, and this method cannot be translated into a store expression.
this is my respective code
DateTime currenteDate = DateTime.Now.Date;
DateTime EndDate = DateTime.Now.Date .AddDays(3);
ViewBag.TopRecenteFiles = db.Files.Where(y => DateTime.Parse(y.DueDate)> = currenteDate&& DateTime.Parse(y。 DueDate)< = EndDate& y.UserID == user.ID)。ToList()。Take(7);
DateTime currenteDate = DateTime.Now.Date;
DateTime EndDate = DateTime.Now.Date.AddDays(3);
ViewBag.TopRecenteFiles = db.Files.Where(y => DateTime.Parse(y.DueDate) >= currenteDate && DateTime.Parse(y.DueDate) <= EndDate && y.UserID == user.ID).ToList().Take(7);
所以任何人都可以帮助我
提前感谢
so can any one help me out
thanks in advance
推荐答案
好吧,这看起来非常简单:
Well, that seems pretty straightforward:
// Note the changes from your code here
DateTime currentDate = DateTime.Today;
DateTime endDate = currentDate.AddDays(3);
DateTime dueDate = db.Files.Where(y => (y.DueDate >= currentDate) && (y.DueDate <= endDate) && (y.UserID == user.ID)).ToList().Take(7);
但是,如果我怀疑的话,File对象的DueDate属性不是DateTime而是字符串(由您编写的代码建议),我担心您无法在单个Linq表达式中执行此操作。你将不得不诉诸一个普通的旧foreach循环。类似于:
However, if, like I suspect, the DueDate property of the File object is not a DateTime but a string (as suggested by the code you wrote), I'm afraid you won't be able to do it in a single Linq expression. You will have to resort to a plain old foreach loop. Something like:
DateTime currentDate = DateTime.Today;
DateTime endDate = currentDate.AddDays(3);
List<File> files = new List<File>();
foreach (File file in db.Files) {
DateTime dueDate = DateTime.Parse(file.DueDate);
if ((dueDate >= currentDate) && (dueDate <= endDate) && (file.UserId == user.ID)) {
files.Add(file);
}
}
// Here you have you File collection.
files = files.Take(7);
结论:当你有DateTime值时,使用DateTime类型,而不是字符串。
Conclusion: when you have DateTime values, use the DateTime type, not the string one.
这篇关于LINQ to Entities无法识别方法'System.DateTime Parse(System.String)'方法,并且此方法无法转换为存储表达式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!