How to compare only date components from DateTime in EF?

后端 未结 14 966
野性不改
野性不改 2020-11-27 17:06

I am having two date values, one already stored in the database and the other selected by the user using DatePicker. The use case is to search for a particular date from the

相关标签:
14条回答
  • 2020-11-27 17:11

    I think this could help you.

    I made an extension since I have to compare dates in repositories filled with EF data and so .Date was not an option since it is not implemented in LinqToEntities translation.

    Here is the code:

            /// <summary>
        /// Check if two dates are same
        /// </summary>
        /// <typeparam name="TElement">Type</typeparam>
        /// <param name="valueSelector">date field</param>
        /// <param name="value">date compared</param>
        /// <returns>bool</returns>
        public Expression<Func<TElement, bool>> IsSameDate<TElement>(Expression<Func<TElement, DateTime>> valueSelector, DateTime value)
        {
            ParameterExpression p = valueSelector.Parameters.Single();
    
            var antes = Expression.GreaterThanOrEqual(valueSelector.Body, Expression.Constant(value.Date, typeof(DateTime)));
    
            var despues = Expression.LessThan(valueSelector.Body, Expression.Constant(value.AddDays(1).Date, typeof(DateTime)));
    
            Expression body = Expression.And(antes, despues);
    
            return Expression.Lambda<Func<TElement, bool>>(body, p);
        }
    

    then you can use it in this way.

     var today = DateTime.Now;
     var todayPosts = from t in turnos.Where(IsSameDate<Turno>(t => t.MyDate, today))
                                          select t);
    
    0 讨论(0)
  • 2020-11-27 17:13

    Use the class EntityFunctions for trimming the time portion.

    using System.Data.Objects;    
    
    var bla = (from log in context.Contacts
               where EntityFunctions.TruncateTime(log.ModifiedDate) ==  EntityFunctions.TruncateTime(today.Date)
               select log).FirstOrDefault();
    

    Source: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/84d4e18b-7545-419b-9826-53ff1a0e2a62/

    UPDATE

    As of EF 6.0 and later EntityFunctions is replaced by DbFunctions.

    0 讨论(0)
  • 2020-11-27 17:16

    You can also use this:

    DbFunctions.DiffDays(date1, date2) == 0

    0 讨论(0)
  • 2020-11-27 17:18

    you can use DbFunctions.TruncateTime() method for this.

    e => DbFunctions.TruncateTime(e.FirstDate.Value) == DbFunctions.TruncateTime(SecondDate);
    
    0 讨论(0)
  • 2020-11-27 17:20

    This is how I do this.

    DateTime date_time_to_compare = DateTime.Now;
    //Compare only date parts
    context.YourObject.FirstOrDefault(r =>
                    EntityFunctions.TruncateTime(r.date) == EntityFunctions.TruncateTime(date_to_compare));
    
    0 讨论(0)
  • 2020-11-27 17:20

    You can user below link to compare 2 dates without time :

    private bool DateGreaterOrEqual(DateTime dt1, DateTime dt2)
            {
                return DateTime.Compare(dt1.Date, dt2.Date) >= 0;
            }
    
    private bool DateLessOrEqual(DateTime dt1, DateTime dt2)
            {
                return DateTime.Compare(dt1.Date, dt2.Date) <= 0;
            }
    

    the Compare function return 3 different values: -1 0 1 which means dt1>dt2, dt1=dt2, dt1

    0 讨论(0)
提交回复
热议问题