LINQ的 - 从日期时间选择日期 [英] Linq - Select Date from DateTime

查看:184
本文介绍了LINQ的 - 从日期时间选择日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的LINQ 结果从中我需要选择只有日期的DateTime 结果
我的查询是这样的:

  VAR UserTemplates =(从XX在VDC.SURVEY_TEMPLATE 
,其中xx.USER_ID ==用户用户名与放大器;&安培; xx.IS_ACTIVE == 1
选择新的
{
xx.TEMPLATE_ID,
XX .TEMPLATE_NAME,
// CREATED_DATE = xx.CREATED_DATE.Value.Date
// CREATED_DATE = EntityFunctions.TruncateTime(xx.CREATED_DATE)
xx.CREATED_DATE
})了ToList( );



这可能吗?
任何帮助,将不胜感激。



  CREATED_DATE  - `datetime`数据类型

事实上,我将它绑定到一个控制作为一种数据源和控制同时显示日期和时间。但我想只显示日期



当我与 CREATED_DATE = xx.CREATED_DATE.Value.Date 尝试,这是给像一个错误:




指定类型的成员'日期'不是在LINQ支持实体。
只有初始化,实体成员和实体导航属性
的支持。



解决方案

如果它是演示的需要,那么你可以使用 DataFormatString 属性。例如,如果你是一个数据源绑定到一个GridView,你可以做的;

 < ASP:BoundField的数据字段= CREATED_DATE...... 
DataFormatString ={0:D}../>



否则,你可以使用的 EntityFunctions.TruncateTime()返回输入日期没有时间部分。

  EntityFunctions.TruncateTime(xx.CREATED_DATE)

您的查询会是这样;

  VAR UserTemplates =(来自XX的VDC。问卷调查_ 
,其中xx.USER_ID ==用户用户名与放大器;&安培; xx.IS_ACTIVE == 1
选择新的
{
xx.TEMPLATE_ID,
xx.TEMPLATE_NAME,
EntityFunctions.TruncateTime(xx.CREATED_DATE)//新像
})了ToList()。


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

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.

When I'm trying with CREATED_DATE = xx.CREATED_DATE.Value.Date,It is giving an error like :

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

解决方案

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}" ../>

Else you can use EntityFunctions.TruncateTime() which returns the input date without the time portion.

EntityFunctions.TruncateTime(xx.CREATED_DATE)

Your query would be like;

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的 - 从日期时间选择日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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