gpt4 book ai didi

c# - Linq - 从日期时间中选择日期

转载 作者:太空狗 更新时间:2023-10-29 18:05:57 25 4
gpt4 key购买 nike

我有一个 Linq 结果,我只需要从 DateTime 中选择 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,
//CREATED_DATE = xx.CREATED_DATE.Value.Date
//CREATED_DATE = EntityFunctions.TruncateTime(xx.CREATED_DATE)
xx.CREATED_DATE
}).ToList();

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

CREATED_DATE - `datetime` datatype

实际上,我将它作为 DataSource 绑定(bind)到一个控件,该控件同时显示 Date 和 Time。但我只想显示 date

当我尝试使用 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.

最佳答案

如果是为了演示目的,那么您可以使用DataFormatString 属性。例如,如果您将数据源绑定(bind)到 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();

关于c# - Linq - 从日期时间中选择日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19021606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com