如何在Linq查询中将DateTime转换为String? [英] How can I convert DateTime to String in Linq Query?

查看:488
本文介绍了如何在Linq查询中将DateTime转换为String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须以MMM dd,YYYY格式显示日期.

I have to display date in MMM dd,YYYY format.

var performancereviews = from pr in db.PerformanceReviews
                                         .Include(a => a.ReviewedByEmployee)
                                 select new PerformanceReviewsDTO
                                 {
                                     ReviewDate=pr.ReviewDate.ToString("MMM dd,yyyy"),
                                     EmployeeName=pr.ReviewedByEmployee.Name,
                                     JobTitle=pr.ReviewedByEmployee.JobTitle,
                                     ReviewerComments=pr.CommentsByReviewer,
                                     EmployeeComments=pr.CommentsByEmployee
                                 };

这是我收到的错误消息

ExceptionMessage:LINQ to Entities无法识别方法'System.String ToString(System.String)',并且该方法无法转换为商店表达式. ExceptionType: System.NotSupportedException

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. ExceptionType: System.NotSupportedException

pr.ReviewDate上应用ToString时出现错误.

请以正确的解决方案指导我如何完成此任务.我知道在普通的C#编码中有几种可用的选项,但是在Linq中我们该怎么做.

Kindly guide me with proper solution how can I accomplish this. I know in normal C# coding there are several options available but in Linq how can we do it.

推荐答案

之所以会这样,是因为LINQ to Entities试图将表达式树转换为SQL查询,而.ToString() 可以转换为SQL,.ToString(string) 不能. (SQL没有相同的字符串格式概念.)

This is happening because LINQ to Entities is trying to convert the expression tree into a SQL query, and while .ToString() can be translated into SQL, .ToString(string) can not. (SQL doesn't have the same concepts of string formatting.)

要解决此问题,请不要在查询中执行格式化,而是在显示逻辑中执行格式化.保持查询尽可能简单:

To resolve this, don't perform the formatting in the query, perform it in the display logic. Keep the query as simple as possible:

select new PerformanceReviewsDTO
{
    ReviewDate=pr.ReviewDate,
    EmployeeName=pr.ReviewedByEmployee.Name,
    JobTitle=pr.ReviewedByEmployee.JobTitle,
    ReviewerComments=pr.CommentsByReviewer,
    EmployeeComments=pr.CommentsByEmployee
}

在这种情况下,PerformanceReviewsDTO.ReviewDate仍然是DateTime值.它不是在格式化数据,而只是携带数据. (就像DTO一样.)

In this case PerformanceReviewsDTO.ReviewDate is still a DateTime value. It's not formatting the data, just carrying it. (Like a DTO should.)

然后当您显示值时,执行格式化.例如,这是否在MVC视图中使用?:

Then when you display the value, perform the formatting. For example, is this being used in an MVC view?:

@Model.ReviewDate.ToString("MMM dd,yyyy")

您甚至可以向PerformanceReviewsDTO添加一个简单的属性以用于格式化显示:

You might even just add a simple property to PerformanceReviewsDTO for the formatted display:

public string FormattedReviewDate
{
    get { return ReviewDate.ToString("MMM dd,yyyy"); }
}

然后,绑定到DTO上的属性的任何东西都可以绑定到该对象(假设在这种情况下是单向绑定).

Then whatever is binding to properties on the DTO can just bind to that (assuming it's a one-way binding in this case).

这篇关于如何在Linq查询中将DateTime转换为String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆