Date compare in LINQ
i was looking for date compare in LINQ and i found a开发者_运维技巧 code from searching google.
var currentDate = DateTime.Now.Date;
VisitorLog log = db.Context.VisitorLogs .Where(vl=>vl.inDate.Date == currentDate).FirstOrDefault();
i need to know the above code works fine or not.
how do i compare date in particular format like i compare date in sql server
compare(varchar(10),mydatefield,112)>=compare(varchar(10),@mydatefield,112)
so please guide me how could i compare date using linq in particular format like above one.
thanks
The thing is that you will rarely compare dates in a specific format(unless you want to compare them as strings or something). Thus is the introduction of the DateTime structure.
Anyway the most used way of comparing dates is not to check for equality but to actually check whether a date comes into a specific range. For example:
startDate <= order.OrderDate && order.OrderDate < endDate
Can this link help you?
How to compare dates in LINQ?
where t.CreateDate.Date < DateTime.Today.AddMonths(-1)
The format matters when you parse a string to a DateTime object. Once you have done that and you have two DateTime objects then comparing their Date properties is the correct thing to do. The Date properties return DateTime objects with time set to 12:00am
In order to parse the date correctly you can pass a IFormatProvider to the DateTime.Parse() method. E.g. a CultureInfo object:
DateTime d = DateTime.Parse("07/04/2011", CultureInfo.GetCultureInfo("en-us"));
精彩评论