LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method

Viewed 42823

I am trying to convert one application to EntityFrameWork codefirst. My present code is

 string sFrom  ="26/12/2013";
 select * FROM Trans where  CONVERT(datetime, Date, 105) >=   CONVERT(datetime,'" + sFrom + "',105) 

And i tried

 DateTime dtFrom = Convert.ToDateTime(sFrom );
TransRepository.Entities.Where(x  =>Convert.ToDateTime(x.Date) >= dtFrom) 

But I got an error like this

LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method

Please help Thanks in advance

4 Answers

Take the convert part out of linq statement and keep it in a variable, like this:

var xyz = Convert.ToDateTime("12/31/2018");

and use this variable in the linq statement.

/i had a similar problem where i was getting a search string and querying a datetime column... see line 4/

1)case "Admission_Date":
2)if (!String.IsNullOrEmpty(SearchValue))
3)   {
4)   DateTime dtFrom = 
Convert.ToDateTime(SearchValue); 
wards = from s in db.CORE_DATA_tbl
    where s.Admit_Date == dtFrom
    orderby s.ActionType, s.AdmissionLocation
    select s;
    }
    break;
Related