简体   繁体   English

实体框架DateTime要字符串吗?

[英]Entity Framework DateTime to string?

I'm trying to convert a datetime to a string, I'm using a foreach. 我正在尝试将日期时间转换为字符串,我正在使用foreach。

My code: 我的代码:

@foreach (var item in db.FrontTexts.Where(i => i.Placeholder == "Privacy"))
{
    <h2>@item.Heading</h2>
    @Html.Raw(item.Text)
    <p>This document was last updated: @item.LastUpdated.ToString("dd/MM/yyyy")</p>
}

It gives me a beautiful red line under "dd/MM/yyyy" saying: 它在“ dd / MM / yyyy”下给了我一条漂亮的红线:

Method "ToString" has 0 parameter(s) but is invokved with 1 argument. 方法“ ToString”具有0个参数,但是调用了1个参数。

When doing normal queries with WebMatrix.Data you can easily convert DateTime's to a string like above, so how would I achieve this in entity framework? 使用WebMatrix.Data进行常规查询时,您可以轻松地将DateTime转换为上述字符串,那么如何在实体框架中实现呢?

For Nullable types there is ToString() method without parameter. 对于Nullable类型,有没有参数的ToString()方法。 so you have to first check it's not null and after that call ToString() method: 因此,您必须首先检查它是否不为null,然后再调用ToString()方法:

<p>This document was last updated: @if(item.LastUpdated.HasValue){<text>@item.LastUpdated.Value.ToString("dd/MM/yyyy")</text>}else{<text>N/A</text>}</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM