Share via


How to compare dates using LINQ?

Question

Sunday, March 10, 2013 3:38 AM

Hi

I want to compare between two dates but date is saving in database as string and type is varchar2 and format is 2013/03/05. How to compare between two dates when date is saved as string in database? I am using CompareTo but i don't know how to proceed?

Thanks

All replies (10)

Saturday, March 23, 2013 8:10 AM ✅Answered

hi

this code is working fine for me

DateTime dt = Convert.ToDateTime(DateTime.Today.AddDays(-30).ToShortDateString(););
            _dateFrom = dt.ToString("yyyy/MM/dd");
            _dateTo = DateTime.Today.ToString("yyyy/MM/dd");
            Expression<Func<RCOMTRAN, bool>> where = null;
            where = param => Convert.ToDateTime(param.BrnEntryDate) >= Convert.ToDateTime(_dateFrom) && Convert.ToDateTime(param.BrnEntryDate) <= Convert.ToDateTime(_dateTo);

Saturday, March 23, 2013 5:43 PM ✅Answered

Your code is equivalent to this

DateTime lastMonth = DateTime.Today.AddMonths(-1);
Expression<Func<RCOMTRAN, bool>> where = param => Convert.ToDateTime(param.BrnEntryDate) >= lastMonth &&
                                                  Convert.ToDateTime(param.BrnEntryDate) <= DateTime.Today;

You really don't need to do all that converting of dates to strings and back again.  It makes the code difficult to understand and it can just lead to localization problems in the future.
Think about the date which which is 30 days in the past.   In the first line you convert that date to a string (ToShortDateString).  Then you convert it back from a string to a DateTime (Convert.ToDateTime).  The result of these two operations cancel each other.  In the next line you convert that date back to a string (.ToString).  In the fifth line you convert that string back to the original date (Conver.ToDateTime) again, these two operations cancel each other.  The total effort is four conversions with a net result of no change!


Sunday, March 10, 2013 4:20 AM

Convert the strings to DateTime values by using ParseExact and then you can just use >, <, or whatever.


Sunday, March 10, 2013 7:42 AM

Well use 

DateTime date=Convert.ToDateTime("your string");
then check like this :
If date.Value.Month    and many other options like this.

Sunday, March 10, 2013 9:36 AM

Hi,

Check below URL,

http://stackoverflow.com/questions/10093359/comparing-dates-in-query-using-linq

http://stackoverflow.com/questions/6568748/date-compare-in-linq

 


Sunday, March 10, 2013 10:00 AM

Convert the value to datetime and then use >< operators as you wish,

DateTime.ParseExact(Field,"yyyy/MM/dd")>dateparameter


Sunday, March 10, 2013 11:30 AM

As mentioned, you'll just need to parse the two dates properly and then they can be compared with using traditional operators (greater than, less than, etc.)

You can use DateTime.ParseExact() to handle parsing your string value using a specific format as shown below : 

//Parse each of your Dates (using DateTime.ParseExact() to convert the string to a Date)
DateTime dateTimeOne = DateTime.ParseExact("YourFirstDateTime","yyyy/MM/dd");
DateTime dateTimeTwo = DateTime.ParseExact("YourSecondDateTime","yyyy/MM/dd");

//Perform your Comparison
if(dateTimeOne > dateTimeTwo)
{
     //Your Logic Here
}
else
{
     //Your Other Logic Here
}

Sunday, March 10, 2013 4:44 PM

DateTime.ParseExact takes at least three parameters.


Sunday, March 10, 2013 5:21 PM

As per Paul's correction, DateTime.ParseExact() does take 3 parameters (at least) instead of the two that I had previously mentioned (I always seem to forget that one, as it can also accept null).: 

//Your Culture
CultureInfo provider = CultureInfo.InvariantCulture;

//Parse each of your Dates (using DateTime.ParseExact() to convert the string to a Date)
DateTime dateTimeOne = DateTime.ParseExact("YourFirstDateTime","yyyy/MM/dd", provider);
DateTime dateTimeTwo = DateTime.ParseExact("YourSecondDateTime","yyyy/MM/dd", provider);

//Perform your Comparison
if(dateTimeOne > dateTimeTwo)
{
     //Your Logic Here
}
else
{
     //Your Other Logic Here
}

and as mentioned, using null instead of specifying the culture will simply use the current culture : 

//Parse each of your Dates (using DateTime.ParseExact() to convert the string to a Date)
DateTime dateTimeOne = DateTime.ParseExact("YourFirstDateTime","yyyy/MM/dd", null);
DateTime dateTimeTwo = DateTime.ParseExact("YourSecondDateTime","yyyy/MM/dd", null);

Monday, March 11, 2013 2:40 AM

hi

i have where expression as follows

Expression<Func<RCOMTRAN, bool>> where = null;
where = param => param.BrnEntryDate ........................ (how to implement here)

BrnEntryDate is my column in table where date is being saved as string. Now how to complete the Expression. 

Thanks