DateTime.ToString("MM/dd/yyyy") inside linq is throwing an error. Here is my code:
public IEnumerable<EmployeeViewModel> GetAllEmployees()
{
List<EmployeeViewModel> vm = (
from a in db.Employees
select new EmployeeViewModel
{
EmployeeId = a.EmployeeId,
EmployeeName = a.EmployeeName,
HiredDateString = a.HiredDateTime.ToString("MM/dd/yyyy"),
HiredTimeString = a.HiredDateTime.ToString("h:mm tt"),
Description= a.Description
}).ToList();
return vm;
}
EDIT: HiredDateString and HiredTimeString are strings inside the ViewModel.
For which I got following error:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
</ExceptionMessage>
<ExceptionType>System.NotSupportedException</ExceptionType>
If I simply use: a.HiredDateTime.ToString() it won't throw an error. But it gives me full DateTime string; I want to get date and time strings separately. Thank you so much for the help.