【发布时间】:2017-01-20 08:30:09
【问题描述】:
我正在加入三张桌子,我想在没有时间的情况下约会。
var query = from anObjective in db.Objective
join anObjectiveType in db.ObjectiveType
on anObjective.IdType equals anObjectiveType.IdObjectiveType
join statusCode in db.StatusCode
on anObjective.IdStatusCode equals statusCode.IdStatus
select new
{
IdObjective = anObjective.IdObjective,
ObjectiveName = anObjective.ObjectiveName,
DateCreation = anObjective.DateCreation, //get just Date without time
DateEnd = anObjective.DateEnd, //get just Date without time
};
我知道有一个方法DbFunctions.TruncateTime,但它返回的时间值为零。但我只想得到没有时间的约会。
我也见过this answer,但是我不知道在加入时如何申请,而不是分组。
模型类:
public partial class Objective
{
public int IdObjective { get; set; }
public string ObjectiveName { get; set; }
public Nullable<System.DateTime> DateCreation { get; set; }
public Nullable<System.DateTime> DateEnd { get; set; }
}
anObjective.DateCreation和anObjective.DateEnd等属性如何无时间获取Date值?
我想要的结果就是 Date: 1.9.2016
编辑:
如果我编写以下语法:
DateCreation =anObjective.DateCreation.Date,
//or
DateCreation =null ? default(DateTime?) : anObjective.DateCreation.Date,
那么我有一个这样的例外:
'System.Nullable' 不包含对 'Date' 并且没有扩展方法 'Date' 接受第一个参数 可以找到类型“System.Nullable”(你是 缺少 using 指令或程序集引用?)
如果我写:
DateCreation =anObjective.DateCreation.Value.ToShortDateString(),
anObjective.DateEnd.Value.ToShortDateString(),
那么我有一个这样的例外:
LINQ to Entities 无法识别方法 'System.String ToShortDateString()' 方法,并且这个方法不能翻译成 商店表达式
如果我写:
DateCreation =anObjective.DateCreation.Value.Date,
那么我有一个这样的例外:
LINQ to Entities 不支持指定的类型成员“日期”。 只有初始化器、实体成员和实体导航属性 支持。
【问题讨论】:
-
您是否尝试过使用
DateTime.Date属性?并不是说您似乎要加入DateCreation或DateEnd,顺便说一句... -
您是否尝试在日期选择后添加“.Date”?例如anObjective.DateCreation.Date
-
@StepUp:不,我的意思是使用您获得的
DateTime值中的Date属性。我们不知道该属性可以为空,所以听起来您可能想要DateCreation = anObjective.DateCreation?.Date等。 -
我看不出你真正想要的是什么 -
DbFunctions.TruncateTime是不支持的DateTime.Date属性的 EF 查询等效项。DateTime.Date也以零时间返回DateTime。 -
您不能在 DateTime 字段中输入 date。 C# 和 .NET 中没有没有日期类型。