Linq - 从DateTime中选择日期

时间:2013-09-26 07:08:35

标签: c# .net sql linq datetime

我有Linq结果,我只需从Date中选择DateTime
我的查询是这样的:

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();

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

CREATED_DATE - `datetime` datatype

实际上,我将它作为DataSource绑定到控件,控件显示Date and Time。但我只想显示date

当我尝试使用CREATED_DATE = xx.CREATED_DATE.Value.Date时,它会出现如下错误:

  

LINQ to Entities不支持指定的类型成员“Date”。   仅初始化程序,实体成员和实体导航属性   得到支持。

5 个答案:

答案 0 :(得分:18)

如果是出于演示目的,那么您可以使用DataFormatString属性。例如,如果要将数据源绑定到GridView,则可以执行;

<asp:BoundField DataField="CREATED_DATE" ...
     DataFormatString="{0:d}" ../>

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

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();

答案 1 :(得分:1)

使用它...它对我有用..

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=SqlFunctions.DateName("day", xx.CREATED_DATE).Trim() + "/" +
        SqlFunctions.StringConvert((double)xx.CREATED_DATE.Value.Month).TrimStart() + "/" +
        SqlFunctions.DateName("year", xx.CREATED_DATE)
         }).ToList();

输出日期为dd / MM / yyyy格式

答案 2 :(得分:0)

也许这个?

var qry = (from xx in VDC.SURVEY_TEMPLATE
           where xx.USER_ID == userid && xx.IS_ACTIVE == 1
           select new { xx.TEMPLATE_ID, xx.TEMPLATE_NAME, xx.xx.CREATED_DATE });

var UserTemplates = qry.AsEnumerable().Select(xx => new 
                    {
                       xx.TEMPLATE_ID,
                       xx.TEMPLATE_NAME,
                       xx.CREATED_DATE.Value.Date 
                    }).ToList();

答案 3 :(得分:0)

var UserTemplates = (from xx in VDC.SURVEY_TEMPLATE
                     where xx.USER_ID == userid && xx.IS_ACTIVE == 1 select  xx).AsEnumerable()
                     .Select( i=> new
                     {
                       i.TEMPLATE_ID,
                       i.TEMPLATE_NAME,
                       CREATED_DATE = i.CREATED_DATE.ToString("M/d/yyyy"),
                       i.CREATED_DATE
                     }).ToList();

答案 4 :(得分:0)

尝试一下

var UserTemplates = (from xx in VDC.SURVEY_TEMPLATE
                 where xx.USER_ID == userid && xx.IS_ACTIVE == 1
                 select new Attributes
                 {
                   TEMPLATE_ID=xx.TEMPLATE_ID,
                   TEMPLATE_NAME=xx.TEMPLATE_NAME,
                   dateCreatd = xx.CREATED_DATE                      
                 })
                 .AsEnumerable()
                 .select(p=>new Attributes
                 {
                   TEMPLATE_ID =p.TEMPLATE_ID,
                   TEMPLATE_NAME=p.TEMPLATE_NAME,
                   dateString = p.dateCreatd .Value.toString("YYYY-MMM-dd")                       
                 }).ToList();


public class Attributes
{
 public string TEMPLATE_ID { get; set; }
 public string TEMPLATE_NAME { get; set }
 public DateTime dateCreatd { get; set; }
 public string dateString { get; set; }

}

希望对您有用