Linq - 从DateTime选择日期 [英] Linq - Select Date from DateTime
问题描述
我有一个 Linq
结果,我只需从 DateTime中选择
。 Date
我的查询如下:
I have a Linq
Result from which I need to select only Date
from DateTime
.
My Query goes like this :
var UserTemplates = (from xx in VDC.SURVEY_TEMPLATE
where xx.USER_ID == userid && xx.IS_ACTIVE == 1
select new
{
xx.TEMPLATE_ID,
xx.TEMPLATE_NAME,
//CREATED_DATE = xx.CREATED_DATE.Value.Date
//CREATED_DATE = EntityFunctions.TruncateTime(xx.CREATED_DATE)
xx.CREATED_DATE
}).ToList();
可能吗?
任何帮助将不胜感激。
Is that Possible? Any help would be greatly appreciated.
CREATED_DATE - `datetime` datatype
实际上,我将它绑定到一个控件,作为 DataSource
,控件是显示日期和时间
。但是我只想显示 date
。
Actually,I'm Binding it to a Control as a DataSource
and the control is displaying both Date and Time
.But I want to display only date
.
当我尝试使用 CREATED_DATE = xx.CREATED_DATE.Value.Date
,它发出如下错误:
When I'm trying with CREATED_DATE = xx.CREATED_DATE.Value.Date
,It is giving an error like :
LINQ to Entities不支持指定的类型成员'Date'。
只支持初始化器,实体成员和实体导航属性
。
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
推荐答案
如果是用于演示目的,则可以使用 DataFormatString
属性。例如,如果你将一个数据源绑定到一个GridView,你可以这样做:
If it is for presentation purpose, then you can use DataFormatString
property. For Example, if you are binding a datasource to a GridView, you could do as;
<asp:BoundField DataField="CREATED_DATE" ...
DataFormatString="{0:d}" ../>
否则,您可以使用 EntityFunctions.TruncateTime(),它返回没有时间部分的输入日期。
Else you can use EntityFunctions.TruncateTime() which returns the input date without the time portion.
EntityFunctions.TruncateTime(xx.CREATED_DATE)
您的查询将如下:
var UserTemplates = (from xx in VDC.SURVEY_TEMPLATE
where xx.USER_ID == userid && xx.IS_ACTIVE == 1
select new
{
xx.TEMPLATE_ID,
xx.TEMPLATE_NAME,
EntityFunctions.TruncateTime(xx.CREATED_DATE) //new like
}).ToList();
这篇关于Linq - 从DateTime选择日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!