entity framework 时间操作

//新方法 entity framework 时间操作 
query.Where(c => System.Data.Entity.DbFunctions.DiffDays(DateTime.Now, c.AddTime) == 0).Count();

//比较天数

//当天用户
//方法已经过时
var useractiveentity = db.UserActive.Where(u => u.UserInfoID == uid && System.Data.Objects.EntityFunctions.DiffDays(DateTime.Now, u.Createtime) == 0).FirstOrDefault(); if (useractiveentity == null) { UserActive userActive = new UserActive(); userActive.Createtime = DateTime.Now; userActive.UserInfoID = uid; db.UserActive.Add(userActive); db.SaveChanges(); }
//比较小时

//当前小时

var useractiveentity = db.UserActive.Where(u => u.UserInfoID == uid && System.Data.Objects.EntityFunctions.DiffHours(DateTime.Now, u.Createtime) == 0).FirstOrDefault();

//比较分钟
//当前分钟
var useractiveentity = db.UserActive.Where(u => u.UserInfoID == uid && System.Data.Objects.EntityFunctions.DiffMinutes(DateTime.Now, u.Createtime) == 0).FirstOrDefault();
 

//System.Data.Objects.EntityFunctions 类中 // 摘要: // 调用 DiffDays 规范函数。 有关 DiffDays 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // dateValue1: // 有效日期。 // // dateValue2: // 有效日期。 // // 返回结果: // dateValue1 和 dateValue2 之间的天数。 [EdmFunction("Edm", "DiffDays")] public static int? DiffDays(DateTime? dateValue1, DateTime? dateValue2); // // 摘要: // 调用 DiffDays 规范函数。 有关 DiffDays 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // dateValue1: // 一个有效的日期时间偏移。 // // dateValue2: // 一个有效的日期时间偏移。 // // 返回结果: // dateValue1 和 dateValue2 之间的天数。 [EdmFunction("Edm", "DiffDays")] public static int? DiffDays(DateTimeOffset? dateValue1, DateTimeOffset? dateValue2); // // 摘要: // 调用 DiffHours 规范函数。 有关 DiffHours 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 有效日期。 // // timeValue2: // 有效日期。 // // 返回结果: // timeValue1 和 timeValue2 之间的小时数。 [EdmFunction("Edm", "DiffHours")] public static int? DiffHours(DateTime? timeValue1, DateTime? timeValue2); // // 摘要: // 调用 DiffHours 规范函数。 有关 DiffHours 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的日期时间偏移。 // // timeValue2: // 一个有效的日期时间偏移。 // // 返回结果: // timeValue1 和 timeValue2 之间的小时数。 [EdmFunction("Edm", "DiffHours")] public static int? DiffHours(DateTimeOffset? timeValue1, DateTimeOffset? timeValue2); // // 摘要: // 调用 DiffHours 规范函数。 有关 DiffHours 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的时间跨度。 // // timeValue2: // 一个有效的时间跨度。 // // 返回结果: // timeValue1 和 timeValue2 之间的小时数。 [EdmFunction("Edm", "DiffHours")] public static int? DiffHours(TimeSpan? timeValue1, TimeSpan? timeValue2); // // 摘要: // 调用 DiffMicroseconds 规范函数。 有关 DiffMicroseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 有效日期。 // // timeValue2: // 有效日期。 // // 返回结果: // timeValue1 和 timeValue2 之间的微秒数。 [EdmFunction("Edm", "DiffMicroseconds")] public static int? DiffMicroseconds(DateTime? timeValue1, DateTime? timeValue2); // // 摘要: // 调用 DiffMicroseconds 规范函数。 有关 DiffMicroseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的日期时间偏移。 // // timeValue2: // 一个有效的日期时间偏移。 // // 返回结果: // timeValue1 和 timeValue2 之间的微秒数。 [EdmFunction("Edm", "DiffMicroseconds")] public static int? DiffMicroseconds(DateTimeOffset? timeValue1, DateTimeOffset? timeValue2); // // 摘要: // 调用 DiffMicroseconds 规范函数。 有关 DiffMicroseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的时间跨度。 // // timeValue2: // 一个有效的时间跨度。 // // 返回结果: // timeValue1 和 timeValue2 之间的微秒数。 [EdmFunction("Edm", "DiffMicroseconds")] public static int? DiffMicroseconds(TimeSpan? timeValue1, TimeSpan? timeValue2); // // 摘要: // 调用 DiffMilliseconds 规范函数。 有关 DiffMilliseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 有效日期。 // // timeValue2: // 有效日期。 // // 返回结果: // timeValue1 和 timeValue2 之间的毫秒数。 [EdmFunction("Edm", "DiffMilliseconds")] public static int? DiffMilliseconds(DateTime? timeValue1, DateTime? timeValue2); // // 摘要: // 调用 DiffMilliseconds 规范函数。 有关 DiffMilliseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的日期时间偏移。 // // timeValue2: // 一个有效的日期时间偏移。 // // 返回结果: // timeValue1 和 timeValue2 之间的毫秒数。 [EdmFunction("Edm", "DiffMilliseconds")] public static int? DiffMilliseconds(DateTimeOffset? timeValue1, DateTimeOffset? timeValue2); // // 摘要: // 调用 DiffMilliseconds 规范函数。 有关 DiffMilliseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的时间跨度。 // // timeValue2: // 一个有效的时间跨度。 // // 返回结果: // timeValue1 和 timeValue2 之间的毫秒数。 [EdmFunction("Edm", "DiffMilliseconds")] public static int? DiffMilliseconds(TimeSpan? timeValue1, TimeSpan? timeValue2); // // 摘要: // 调用 DiffMinutes 规范函数。 有关 DiffMinutes 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 有效日期。 // // timeValue2: // 有效日期。 // // 返回结果: // timeValue1 和 timeValue2 之间的分钟数。 [EdmFunction("Edm", "DiffMinutes")] public static int? DiffMinutes(DateTime? timeValue1, DateTime? timeValue2); // // 摘要: // 调用 DiffMinutes 规范函数。 有关 DiffMinutes 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的日期时间偏移。 // // timeValue2: // 一个有效的日期时间偏移。 // // 返回结果: // timeValue1 和 timeValue2 之间的分钟数。 [EdmFunction("Edm", "DiffMinutes")] public static int? DiffMinutes(DateTimeOffset? timeValue1, DateTimeOffset? timeValue2); // // 摘要: // 调用 DiffMinutes 规范函数。 有关 DiffMinutes 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的时间跨度。 // // timeValue2: // 一个有效的时间跨度。 // // 返回结果: // timeValue1 和 timeValue2 之间的分钟数。 [EdmFunction("Edm", "DiffMinutes")] public static int? DiffMinutes(TimeSpan? timeValue1, TimeSpan? timeValue2); // // 摘要: // 调用 DiffMonths 规范函数。 有关 DiffMonths 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // dateValue1: // 有效日期。 // // dateValue2: // 有效日期。 // // 返回结果: // dateValue1 和 dateValue2 之间的月数。 [EdmFunction("Edm", "DiffMonths")] public static int? DiffMonths(DateTime? dateValue1, DateTime? dateValue2); // // 摘要: // 调用 DiffMonths 规范函数。 有关 DiffMonths 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // dateValue1: // 一个有效的日期时间偏移。 // // dateValue2: // 一个有效的日期时间偏移。 // // 返回结果: // dateValue1 和 dateValue2 之间的月数。 [EdmFunction("Edm", "DiffMonths")] public static int? DiffMonths(DateTimeOffset? dateValue1, DateTimeOffset? dateValue2); // // 摘要: // 调用 DiffNanoseconds 规范函数。 有关 DiffNanoseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 有效日期。 // // timeValue2: // 有效日期。 // // 返回结果: // timeValue1 和 timeValue2 之间的毫微秒数。 [EdmFunction("Edm", "DiffNanoseconds")] public static int? DiffNanoseconds(DateTime? timeValue1, DateTime? timeValue2); // // 摘要: // 调用 DiffNanoseconds 规范函数。 有关 DiffNanoseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的日期时间偏移。 // // timeValue2: // 一个有效的日期时间偏移。 // // 返回结果: // timeValue1 和 timeValue2 之间的毫微秒数。 [EdmFunction("Edm", "DiffNanoseconds")] public static int? DiffNanoseconds(DateTimeOffset? timeValue1, DateTimeOffset? timeValue2); // // 摘要: // 调用 DiffNanoseconds 规范函数。 有关 DiffNanoseconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 有效日期。 // // timeValue2: // 有效日期。 // // 返回结果: // timeValue1 和 timeValue2 之间的毫微秒数。 [EdmFunction("Edm", "DiffNanoseconds")] public static int? DiffNanoseconds(TimeSpan? timeValue1, TimeSpan? timeValue2); // // 摘要: // 调用 DiffSeconds 规范函数。 有关 DiffSeconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 有效日期。 // // timeValue2: // 有效日期。 // // 返回结果: // timeValue1 和 timeValue2 之间的秒数。 [EdmFunction("Edm", "DiffSeconds")] public static int? DiffSeconds(DateTime? timeValue1, DateTime? timeValue2); // // 摘要: // 调用 DiffSeconds 规范函数。 有关 DiffSeconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的日期时间偏移。 // // timeValue2: // 一个有效的日期时间偏移。 // // 返回结果: // timeValue1 和 timeValue2 之间的秒数。 [EdmFunction("Edm", "DiffSeconds")] public static int? DiffSeconds(DateTimeOffset? timeValue1, DateTimeOffset? timeValue2); // // 摘要: // 调用 DiffSeconds 规范函数。 有关 DiffSeconds 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // timeValue1: // 一个有效的时间跨度。 // // timeValue2: // 一个有效的时间跨度。 // // 返回结果: // timeValue1 和 timeValue2 之间的秒数。 [EdmFunction("Edm", "DiffSeconds")] public static int? DiffSeconds(TimeSpan? timeValue1, TimeSpan? timeValue2); // // 摘要: // 调用 DiffYears 规范函数。 有关 DiffYears 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // dateValue1: // 有效日期。 // // dateValue2: // 有效日期。 // // 返回结果: // dateValue1 和 dateValue2 之间的年数。 [EdmFunction("Edm", "DiffYears")] public static int? DiffYears(DateTime? dateValue1, DateTime? dateValue2); // // 摘要: // 调用 DiffYears 规范函数。 有关 DiffYears 规范函数的信息,请参见日期和时间规范函数。 // // 参数: // dateValue1: // 一个有效的日期时间偏移。 // // dateValue2: // 一个有效的日期时间偏移。 // // 返回结果: // dateValue1 和 dateValue2 之间的年数。 [EdmFunction("Edm", "DiffYears")] public static int? DiffYears(DateTimeOffset? dateValue1, DateTimeOffset? dateValue2);

 

posted @ 2015-06-10 08:37  好学Ace  阅读(1090)  评论(0编辑  收藏  举报