Simple way to compare Dates in DateTime attribute using Entity Framework 4 and Linq queries

后端 未结 4 1752
情深已故
情深已故 2020-12-17 00:24

I\'m trying to run the following bit of code, but the comparison fails by not handing my the entities I expect it to.

It\'s comparing 06/09/2011 0:00:00

相关标签:
4条回答
  • 2020-12-17 00:29

    You could convert using

    DateTime.ToShortDateString()
    

    http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx

    This would neglect the Time.

    Then compare the two Strings.

    if(string1 == string2)
    {
     //Do Something
    }
    
    0 讨论(0)
  • 2020-12-17 00:37

    try

    DateTime todayStart = DateTime.Now.Date;
    DateTime todayEnd = DateTime.Now;
    var newAuctionsResults = repo.FindAllAuctions()
                            .Where(a => a.IsActive == true || (a.StartTime.Value >= todayStart && a.StartTime.Value <= todayEnd))
                            .ToList();
    
    0 讨论(0)
  • 2020-12-17 00:39

    You can use the individual members:

    var newAuctionsResults = repo.FindAllAuctions()
                            .Where(a => a.IsActive == true 
                                        || (a.StartTime.Value.Year == todayYear
                                            && a.StartTime.Value.Month == todayMonth
                                            && a.StartTime.Value.Day == todayDay))
                            .ToList();
    

    ...or use any of the other methods/properties supported in L2E.

    0 讨论(0)
  • 2020-12-17 00:51

    You can also use EntityFunctions.TruncateTime() under the namespace System.Data.Objects

    Ex.

    db.Orders.Where(i => EntityFunctions.TruncateTime(i.OrderFinishDate) == EntityFunctions.TruncateTime(dtBillDate) && i.Status == "B")
    

    Works like charm.

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