Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities does not recognize the method Replace? [duplicate]

How to use replace method in entity framework. I use following code but encounter error.

using (SepasProjectEntities sp=new SepasProjectEntities())
{
var query = (from p in sp.HISAccidentLocationMappings
                         where p.Name.Replace('y','x').Contains(txt1.Text)
                         select p
                           ).ToList();
}

An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code

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

like image 670
Raymond Morphy Avatar asked Jan 07 '14 18:01

Raymond Morphy


1 Answers

Based on this MSDN article that contains list of supported methods by Entity Framework - there is just one overload of Replace method that is supported, and it's

System.String Replace(String oldValue, String newValue)

And not

System.String Replace(char oldValue, char newValue)

that you are using. Try to replace it with string version from

Name.Replace('y','x')

to

Name.Replace("y","x")

I didn't try it, but based from documentation it should work

like image 169
Sergey Litvinov Avatar answered Oct 13 '22 02:10

Sergey Litvinov