【问题标题】:How to get Date without Time from DateTime while join tables连接表时如何从 DateTime 获取没有时间的日期
【发布时间】: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.DateCreationanObjective.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 属性?并不是说您似乎要加入DateCreationDateEnd,顺便说一句...
  • 您是否尝试在日期选择后添加“.Date”?例如anObjective.DateCreation.Date
  • @StepUp:不,我的意思是使用您获得的DateTime 值中的Date 属性。我们不知道该属性可以为空,所以听起来您可能想要DateCreation = anObjective.DateCreation?.Date 等。
  • 我看不出你真正想要的是什么 - DbFunctions.TruncateTime 是不支持的 DateTime.Date 属性的 EF 查询等效项。 DateTime.Date 也以零时间返回 DateTime
  • 不能DateTime 字段中输入 date。 C# 和 .NET 中没有没有日期类型。

标签: c# linq


【解决方案1】:

添加“.Date”

   anObjective.DateEnd.Date
   anObjective.DateCreation.Date

【讨论】:

  • 没有这样的属性。我的意思是.Date
  • anObjective.DateCreation.Value.Date ?从更新看来,您的 DateTime 是可空的 所以使用 .Value 来获取 DateTime,然后使用 .Date carful 与空对象引用,如果没有值!
  • 谢谢,我试过了。但是会抛出以下异常The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
  • 现在我看到你确实解决了它。以 0:00:00 作为时间获取日期实际上只是日期。 C# 使用 DateTime 结构来表示日期以及日期和时间。所以这基本上就是你要找到的。这不好吗?你需要一个没有时间的对象吗(没有时间属性,因为正如我所说,应该忽略“零”时间)。
  • 因为日期应该发布到 JavaScript,我不想在 JavaScript 中格式化日期
【解决方案2】:

我在想什么。 DateTime 只是一个整数。两者都减去午夜,它们将相等。

DateTime epoch = new DateTime(1970,1,1,0,0,0);
int dateCreationInt = Math.Floor(dateCreation.Subract(epoch).TotalSeconds/86400);
int dateEndInt = Math.Floor(dateEnd.Subract(epoch).TotalSeconds/86400);

或者更好:

 DateTime epoch = new DateTime(1970,1,1,0,0,0);
int dateCreationInt = Math.Floor(dateCreation.Subract(epoch).TotalDays);
int dateEndInt = Math.Floor(dateEnd.Subract(epoch).TotalDays);

如果您需要 DateTime,只需将整数重新转换为日期时间 - 它们仍将等于午夜,因此将被评估为“相等”:

 dateEnd = epoch.AddDays(dateCreationInt);
 dateCreation = epoch.AddDays(dateEndInt);

现在,如果无论时间如何,两个日期都将匹配。

把它们放在一起

 DateTime epoch = new DateTime(1970,1,1,1);
 select new
{
      IdObjective = anObjective.IdObjective,
      ObjectiveName = anObjective.ObjectiveName,
      DateCreation = epoch.AddDays(anObjective.DateCreation.Subtract(epoch).TotalDays),
      DateEnd = epoch.AddDays(anObjective.DateEnd.Subtract(epoch).TotalDays)

};

【讨论】:

  • 没有这样的属性。我的意思是.Date
  • MSDN 说它从 VS 2010 开始可用(日期)......msdn.microsoft.com/en-us/library/system.datetime(v=vs.100).aspx
  • 谢谢,我试过了。但是会抛出以下异常The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
  • 我有什么:'System.Nullable&lt;System.DateTime&gt;' does not contain a definition for 'Subtract' and no extension method 'Subtract' accepting a first argument of type 'System.Nullable&lt;System.DateTime&gt;' could be found (are you missing a using directive or an assembly reference?)
  • 是的 - 可空性会杀死它 - 你必须使用常规日期时间并在发送之前转换为可空性。
【解决方案3】:

看起来您首先需要 .Value,因为它是可为空的 DateTime。看到这个答案:

Get short date for System Nullable datetime (datetime ?) in C#

之后,您应该能够以与 getshortdatestring 不同的方式获取日期格式。

【讨论】:

  • 谢谢,我试过了。但是会抛出以下异常LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression.
  • 当你在 VS 中输入 .Value 时,Intellisense 是给你任何选择还是根本不识别?如果它是可为空的 DateTime,则 .Value 应该可以工作。此外,您需要检查该值是否实际上为 null,在这种情况下 .Value 将不起作用。
  • 如果我写.Value,那么我就有DateCreation = {5.05.2016 0:00:00}。但我只想得到5.05.2016
  • 正确,但在您输入 .Value 后,输入另一个句点 (.) 并查看您是否从 Intellisense 获得了一些选项。我想你基本上在那里。
  • 尝试任何返回你想要的东西。 。日期? .GetShortDateString?找出最好的应该不难,但我很确定有一个选项(或多个)可以只返回日期时间的日期部分。请记住,要获取日期,您将获得一个字符串。哦,那是 GetShortDateString,而不是您修改后的问题中的 ToShortDateString。
【解决方案4】:

首先,要认识到 .NET 中没有没有时间的日期的内置类型。 At least not yet anyway。因此,如果您想要仅日期,那么您将需要以某种特定格式将其放入字符串中。

您将无法让 LINQ 查询的底层提供者了解 DateTimestring 的转换。因此,您需要以原始形式查询数据,然后在查询结果具体化后将其转换为字符串。

从您的原始查询开始,未修改您在问题顶部显示的内容。然后添加以下内容:

var results = query.AsEnumerable().Select(x=>
new
{
    IdObjective = x.IdObjective,
    ObjectiveName = x.ObjectiveName,
    DateCreation = x.DateCreation.ToShortDateString(),
    DateEnd = x.DateEnd.ToShortDateString()
});

你也有一些选择:

  • 如果您知道您需要ListArray,那么您可以使用ToList()ToArray() 而不是AsEnumerable()

  • 如果您希望生成的字符串采用特定格式或使用特定文化,则可以使用ToString 而不是ToShortDateString

例如,您可能想要一个包含标准 ISO-8601 日期字符串的数组,并且您可能想要使用 Invariant 区域性以避免在当前区域性使用非公历系统时产生副作用。

var results = query.ToArray().Select(x=>
new
{
    IdObjective = x.IdObjective,
    ObjectiveName = x.ObjectiveName,
    DateCreation = x.DateCreation.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
    DateEnd = x.DateEnd.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
});

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 2012-06-22
    • 2015-06-12
    • 2013-07-17
    • 1970-01-01
    • 2011-05-05
    • 2013-12-09
    • 1970-01-01
    相关资源
    最近更新 更多