Introduction
This is most common exception occurs while we are working with entity framework and converting data inside IQueryable result for filtering.
Description
Mainly full exception message as “LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.”.
- var result=dc.Tests.Where(i=>i.status==status.ToString()).FirstOrDefault();
To solve this problem there is a different solution.
Solution 1: All you have to do is move the ToString call to a separate line.
- String userStatus=status.ToString();
- var result=dc.Tests.Where(i=>i.status== userStatus).FirstOrDefault();
- var result=dc.Tests.AsEnumerable().Where(i=>i.status==status.ToString()).FirstOrDefault();
This is only feasible for small entity collections, as in the above method first fetch all database results in memory using AsEnumerable() method and then filtering result. This is not feasible for large Db result.
Solution 3:
Use entity framework extension method SqlFunctions Class or DbFunctions Class,
- var result=dc.Tests.Where(i=>i.status== SqlFunctions.StringConvert
- tatus)).FirstOrDefault();
Note: It's good when the solution with temporary variables is not desirable for whatever reason.
Conclusion
In this article we learned different ways to solve the most common exceptions while working with Entity Framework.

HabibiSoft DubaiPosted Jul 14, 2022, 6:12 AM
I had similar problem in oracle entity framework but didn't work with me, regards, HabibiSoft [email protected]+971 (56) 7271 701
Fernando CostaPosted Apr 23, 2020, 11:43 AM
I have a similar problem. Can you help me? var dataList = this.GetByFilters(filters) .Select(_ => new DataItem { Id = _.AlunoId.ToString(), Name = _.PessoaEscola.Nome + " (" + situacaodoaluno.tostring() + ")",
jhon alexPosted Nov 21, 2018, 10:44 AM
Excelent, Thank you
Bhuvanesh MohankumarPosted May 6, 2016, 3:51 PM
Good one...
Ano MepaniPosted Apr 22, 2016, 1:52 AM
Thank you
Muhammad Aqib ShehzadPosted Apr 21, 2016, 7:39 PM
Nice share