This is because LINQ to Entities is trying to convert the expression tree to an SQL query, and although .ToString() can be translated into SQL, .ToString(string) cannot. (SQL does not have the same concept of formatting strings.)
To solve this problem, do not format in the request, execute it in the display logic. Make the request as simple as possible:
select new PerformanceReviewsDTO { ReviewDate=pr.ReviewDate, EmployeeName=pr.ReviewedByEmployee.Name, JobTitle=pr.ReviewedByEmployee.JobTitle, ReviewerComments=pr.CommentsByReviewer, EmployeeComments=pr.CommentsByEmployee }
In this case, PerformanceReviewsDTO.ReviewDate remains the DateTime value. It does not format the data, just transfers it. (As the DTO should.)
Then, when you display the value, format. For example, is this used in the MVC view ?:
@Model.ReviewDate.ToString("MMM dd,yyyy")
You can even add a simple property to PerformanceReviewsDTO for a formatted display:
public string FormattedReviewDate { get { return ReviewDate.ToString("MMM dd,yyyy"); } }
Then any property binding on the DTO can simply be bound to this (if in this case it is a one-way binding).
David
source share