模型的日期格式不适用于数据类型日期

时间:2018-01-05 16:50:20

标签: asp.net-mvc entity-framework linq date asp.net-mvc-4

您好我在e查询中需要以MM / dd / YYYY格式格式化日期,但它正在工作

我尝试过查询

var query = (from sr in db.StudentRequests
join r in db.Registrations on sr.RegistrationId equals r.RegistrationId
join cc in db.Campus on r.CampusId equals cc.CampusId
join c in db.Classes on sr.ClassId equals c.ClassId
from tc in db.TutorClasses.Where(t => t.ClassId == sr.ClassId).DefaultIfEmpty()
from srt in db.StudentRequestTimings.Where(s => s.StudentRequestId == sr.StudentRequestId).DefaultIfEmpty()
from tsr in db.TutorStudentRequests.Where(t => t.StudentRequestId == srt.StudentRequestId && t.TutorId == registrationid).DefaultIfEmpty()
from r1 in db.Registrations.Where(t => t.RegistrationId == tsr.TutorId).DefaultIfEmpty()
where tc.RegistrationId == registrationid
orderby sr.CreatedOn descending
select new
{   
StatusId = tsr.StatusId == null ? 1 : tsr.StatusId,
Time = db.StudentRequestTimings.Where(p => p.StudentRequestId == sr.StudentRequestId)
.Select(p => p.FromTime.ToString() + "-" + p.ToTime +" " +p.Date+ "<br/>"),

}).ToList().GroupBy(p => new { p.StudentRequestId }).Select(g => g.First()).ToList();

在这里,你可以看到p.date当前显示格式为YYYY / MM / dd格式的时间,但我希望它显示为MM / dd / YYYY格式

在模型方面,我尝试过做

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public Nullable<System.DateTime> Date { get; set; }

但仍无法正常工作

在控制器端,我尝试转换为如下格式

Time = db.StudentRequestTimings.Where(p => p.StudentRequestId == sr.StudentRequestId).
 Select(p => p.FromTime.ToString() + "-" + p.ToTime + " " + Convert.ToString(Convert.ToDateTime(p.Date).ToString("MM/dd/yyyy")) + "<br/>")

但在这种情况下,它会给出错误

附加信息:LINQ to Entities无法识别方法&#39; System.String ToString(System.String)&#39;方法,此方法无法转换为商店表达式。

我还使用了转换为可枚举的

 var model = query.AsEnumerable().Select(x => new TutorDashboard
{
Name = x.Name,
Phone = x.Phone,
Email = x.Email,
ProfilePicture = x.ProfilePicture,
TutorReviewRequestID = x.TutorReviewRequestID,
StudentRequestId = x.StudentRequestId,
RegistrationId = x.RegistrationId,
Location = x.Location,
PaymentMethod = x.PaymentMethod,
CreatedOn = x.CreatedOn,
AcceptedOn = x.AcceptedOn,
ClassName = x.ClassName,
CampusName = x.CampusName,
StripeId = x.StripeId,
AvailableMonth = Month,
AvailableYear = Year,
StatusId = x.StatusId == null ? 1 : x.StatusId,
Time = string.Join("", x.Time),
}).ToList().ToPagedList(page ?? 1, 10);

3 个答案:

答案 0 :(得分:0)

更改此

Convert.ToString(Convert.ToDateTime(p.Date).ToString("MM/dd/yyyy"))

到此:

p.Dated.HasValue? ((DateTime)p.Dated).ToString("dd/MM/yyyy") : ""

答案 1 :(得分:0)

您的LINQ查询被转换为SQL语句,因此编写LINQ存在限制。类型对话包含在这些限制中。

但是,你可以在其他地方进行格式化,就像在View中一样。

@Model.Date.ToString("MM/dd/YYYY");

如果您不想打扰,可以将您的文化设置为enUS并使用.ToShortDateString()

答案 2 :(得分:0)

要使用LINQ to Entities查询中的ToString("format_string")方法,您需要先使用IEnumerable方法实现AsEnumerable()对象:

var time = db.StudentRequestTimings.Where(p => p.StudentRequestId == sr.StudentRequestId)
           .AsEnumerable() // insert this method
           .Select(p => p.FromTime.ToString() + "-" + p.ToTime + " " + Convert.ToString(Convert.ToDateTime(p.Date).ToString("MM/dd/yyyy")) + "<br/>");

然后,将IEnumerable<string>结果传递给第二个查询的匿名类型部分:

var query = (from sr in db.StudentRequests
join r in db.Registrations on sr.RegistrationId equals r.RegistrationId
join cc in db.Campus on r.CampusId equals cc.CampusId
join c in db.Classes on sr.ClassId equals c.ClassId
from tc in db.TutorClasses.Where(t => t.ClassId == sr.ClassId).DefaultIfEmpty()
from srt in db.StudentRequestTimings.Where(s => s.StudentRequestId == sr.StudentRequestId).DefaultIfEmpty()
from tsr in db.TutorStudentRequests.Where(t => t.StudentRequestId == srt.StudentRequestId && t.TutorId == registrationid).DefaultIfEmpty()
from r1 in db.Registrations.Where(t => t.RegistrationId == tsr.TutorId).DefaultIfEmpty()
where tc.RegistrationId == registrationid
orderby sr.CreatedOn descending
select new
{   
    ...
    Time = time,
    ...
}).ToList().GroupBy(p => new { p.StudentRequestId }).Select(g => g.First()).ToList();

如果您有大量存储数据,除了具体化之外,您可以使用视图格式或使用单独的字符串属性来显示格式化日期:

@* Display in view *@
@Model.Date.ToString("MM/dd/yyyy")

// using string property in viewmodel class for formatted DateTime
public string FormattedDate 
{
    get { return Date.ToString("MM/dd/yyyy"); }
}

注意:实体框架的LINQ to Entities尝试将所有查询方法转换为SQL语句,当前ToString("format_string")没有与ToString()不同的与该方法相关联的SQL语句(类似于{ {1}})。

类似问题:

How can I convert DateTime to String in Linq Query?