I want to fine-tune the results in the controller action with a few parameters coming from client.
I was following this answer.
The problem is the logs variable returns no results even if all the parameters are not there (I expect it to return all the records). It works perfectly fine I do
return View(db.Logs.ToList());
But my implementation of the answer by Darren Kopp doesn't return anything at all. My code:
Category cat;
DateTime startDate;
DateTime endDate;
var logs = from log in db.Logs
select log;
if (Enum.TryParse<Category>(viewModel.Category, out cat))
logs = logs.Where(l => l.LogCategory == cat);
if (DateTime.TryParse(viewModel.StartDate, out startDate))
logs = logs.Where(l => l.TimeStamp >= startDate);
return View(logs.ToList());
I am using VS 2015 & this is MVC 5. What is causing the problem?