Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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

Quote:
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
Posted

1 solution

Well, that seems pretty straightforward:
C#
// 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);


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:
C#
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);


Conclusion: when you have DateTime values, use the DateTime type, not the string one.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900