C# Linq - 从 DateTime 中选择日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19021606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq - Select Date from DateTime
提问by RealSteel
I have a Linq
Result from which I need to select only Date
from DateTime
.
My Query goes like this :
我有一个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();
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
.
实际上,我将它作为 a 绑定到一个控件,DataSource
并且该控件同时显示Date and Time
. 但我只想显示date
.
When I'm trying with CREATED_DATE = xx.CREATED_DATE.Value.Date
,It is giving an error like :
当我尝试使用时CREATED_DATE = xx.CREATED_DATE.Value.Date
,它给出了如下错误:
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
LINQ to Entities 不支持指定的类型成员“日期”。仅支持初始值设定项、实体成员和实体导航属性。
采纳答案by Kaf
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;
如果是为了演示目的,那么您可以使用DataFormatString
属性。例如,如果您将数据源绑定到 GridView,您可以这样做;
<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()返回没有时间部分的输入日期。
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();
回答by Dbuggy
Perhaps this ?
也许这个?
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();
回答by Damith
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();
回答by Dharmendra Prajapati
Use this...It worked for me..
使用这个......它对我有用..
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();
And Output Date will be dd/MM/yyyy format
并且输出日期将为 dd/MM/yyyy 格式
回答by Karthic G
Try this,
尝试这个,
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; }
}
hopes it is useful to you
希望对你有用