如何获得日期没有时间从DateTime而连接表



我正在连接三个表,我想获得日期而不是时间。

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,但它返回零值的时间。但是我想要的只是没有时间的约会。

我也看到了这个答案,但是我不知道如何在加入时应用,而不是分组。

模型类:

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

我想要的结果只是日期:1.9.2016

编辑:


如果我写下面的语法:

DateCreation =anObjective.DateCreation.Date,
//or
DateCreation =null ? default(DateTime?) :  anObjective.DateCreation.Date,

那么我就有了这样一个例外:

"系统。null '不包含的定义'Date'和不接受第一个参数的'Date'扩展方法类型的系统。null '可以被找到缺少using指令或程序集引用?)


DateCreation =anObjective.DateCreation.Value.ToShortDateString(),
anObjective.DateEnd.Value.ToShortDateString(),

那么我就有了这样一个例外:

LINQ to Entities不能识别方法System。字符串ToShortDateString()'方法,并且该方法不能转换为存储表达式

如果我写:

DateCreation =anObjective.DateCreation.Value.Date,

那么我就有了这样一个例外:

在LINQ to Entities中不支持指定的类型成员'Date'。只有初始化器、实体成员和实体导航属性支持。

首先,要认识到。net中没有内置日期类型而没有时间。至少现在还没有。所以如果你想要日期,那么你需要一个特定格式的字符串。

您将无法获得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)
});

add "。日期"

   anObjective.DateEnd.Date
   anObjective.DateCreation.Date

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

获取c#中系统空日期(datetime ?)的短日期。

您应该能够以不同于getshortdatestring的方式获取日期格式。

我在想什么呢?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)
};

最新更新