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

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


when you do this:

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

LINQ to Entities cannot translate most .NET Date methods (including the casting you used) into SQL since there is no equivalent SQL. What you need to do is to do below:

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

but wait the above query will fetch entire data, and perform .Where on it, definitely you don't want that,

simple soultion would be this, personally, I would have made my Entity field as DateTime and db column as DateTime

but since, your db/Entity Date field is string, you don't have any other option, other than to change your field in the entity and db to DateTime and then do the comparison

linq Exception: LINQ to Entities does not recognize the method It is the most common exception occurs when working with entity framework and converting data inside IQueryable result for filtering. using (var context = new CustomerContext ()) { var item = context.InvoiceItems.Where (i => i.Code == code.ToString ()).FirstOrDefault (); }


Why is your date column a string? Shouldn't it be a DateTime?

Regardless, if you attempt to perform conversions using .NET functions in a .Where statement against a repository of entities, you'll get that error.

Your best option would be to change that column from a string to a DateTime and proceed from there. If you do, the .Where statement would be:

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

LINQ to Entities does not recognize the method 'System.String ToString ()' method, and this method cannot be translated into a store expression.


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.

LINQ to Entities does not recognize the method 'System.String ToString ()' method, and this method cannot be translated into a store expression.


/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;

LINQ to Entities does not recognize the method 'Int32 ToInt32 (System.Object)' method, and this method cannot be translated into a store expression.


LINQ to Entities does not recognize the method 'System.String ToString()' method and this method cannot be translated into a store expression 86 LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)'


LINQ to Entities does not recognize the method 'Boolean IsCharityMatching (System.String, System.String)' method, and this method cannot be translated into a store expression. I've read lots of previous questions where people get the same error, and if I understand this correctly it's because LINQ to Entities requires the whole linq query expression to be translated to a server query, and therefore you can't call an outside method in it.


LINQ to Entities does not recognize the method 'System.String ToString ()' method, and this method cannot be translated into a store expression. | The ASP.NET Forums LINQ to Entities does not recognize the method 'System.String ToString ()' method, and this method cannot be translated into a store expression.