I have a date in the string format in a table and I need to compare it with the parameter to retrieve all rows that match with the given date. how can I do this? I can’t use DateTime.Parse and Convert.ToDateTime. both of them are not supported (I get an error). Is this possible at all in Linq OR do I have to write a stored procedure for it due to this limitation?
Note: I can’t change the datatype of the column from varchar to DateTime, because that column contains other values as well, not just dates. it is a generic table and the column may contain different types of values based on the values in other columns. changing this design is outside the scope of this question.
sample code (“x” is the date parameter passed into this method):
from t1 in Table1
where EntityFunctions.DiffDays(DateTime.Parse(t1.Value),x) == 0
select new {t1.Col1, t1.Col2};
Update: if I use DateTime.Parse, I get the error “LINQ to Entities does not recognize the method ‘System.DateTime Parse(System.String)’ method, and this method cannot be translated into a store expression.” similar error for Convert.ToDateTime as well.
(Replicating my comment to paste some sample code)
If the date is in string format, couldn’t you apply a
ToStringon yourDateTime(presumablyx) and then do a string comparison?Since you are working on string representations, you need to take care of several issues that would otherwise be handled transparently by
DateTime, including:dd/MM/yyyyvsMM/dd/yyyy).01/01/2011vs1/1/2001).01/01/2011vs01/01/11).2011-01-01 23:30 -01:00would actually be2011-01-02.The sample code below will work if all your dates are in US format, with two-digit days and months, four-digit years, and no timezone offsets.
Edit: Alternate solution:
If you can define a view in your database, you can circumvent the problem by casting your
VARCHARtoDATETIMEselectively. I’m assuming thatValueis the name of your date column.Then, in your LINQ, do a simple
DateTimeequality check: