I am developing MVC application with Searching, Paging. I have UploadDate calendar control for filter purpose with many other fields. This may be null or may contain date.
How to write LINQ where clause that retrieves records whose UploadDate (column name) equals to given UploadDate on form.
public ActionResult MultipleSearch(string searchByUserName, string searchByReaderName, int? searchByReaderType, string searchByUploadDate, int? page)
{
return View(db.TestUploadData2.Where(a =>
(String.IsNullOrEmpty(searchByUserName) || a.UserName.StartsWith(searchByUserName)) &&
(String.IsNullOrEmpty(searchByReaderName) || a.ReaderName.StartsWith(searchByReaderName)) &&
(String.IsNullOrEmpty(searchByUploadDate) || a.UploadDate.ToShortDateString() == searchByUploadDate)
).Where(var => var.ReaderTypeId == searchByReaderType || var.ReaderTypeId > 0).ToList().ToPagedList(page ?? 1, 100));
}
With this approach, getting error as "
LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression.
"
Replacing
(String.IsNullOrEmpty(searchByUploadDate) || a.UploadDate.ToShortDateString() == searchByUploadDate )
With
(String.IsNullOrEmpty(searchByUploadDate) || DateTime.Compare(a.UploadDate, Convert.ToDateTime(searchByUploadDate)) == 0)
gives error as
LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression.
Please give suggestion.
What I have tried:
Googled a lot for this issue but can't find suitable solution. Please note that searchByUploadDate parameter may be null or may contain date...