LINQ to Entities无法识别方法Replace? [重复]

时间:2021-01-18 02:14:00

This question already has an answer here:

这个问题在这里已有答案:

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

如何在实体框架中使用replace方法。我使用以下代码但遇到错误。

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

System.Data.Entity.dll中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理

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.

附加信息:LINQ to Entities无法识别方法'System.String Replace(Char,Char)'方法,并且此方法无法转换为存储表达式。

3 个解决方案

#1


9  

I usually use the other format of LINQ queries, like this:

我通常使用其他格式的LINQ查询,如下所示:

using (SepasProjectEntities sp = new SepasProjectEntities())
{
    var query = sp.HISAccidentLocationmappings
                    .Where(p => p.Name != null
                        && p.Name
                            .Replace("y", "x")
                            .Contains(txt1.Text))
                    .ToList();
}

Replace(char, char) won't work but Replace(string, string) will. Contains(string) should also work.

替换(char,char)将不起作用,但Replace(字符串,字符串)将。包含(字符串)也应该有效。

#2


11  

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

基于这篇包含Entity Framework支持的方法列表的MSDN文章 - 只支持一个Replace方法的重载,它是

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

我没有尝试过,但根据文档它应该工作

#3


1  

Can you flip it around? By that, I'm asking if you can do the replace on the txt1.Text value (and store it in a local variable), then compare it to the value in the database (I'm pretty sure String.Contains IS supported so long as you use the single argument overload).

你能翻转一下吗?通过这个,我问你是否可以对txt1.Text值进行替换(并将其存储在局部变量中),然后将其与数据库中的值进行比较(我非常确定String.Contains是否支持所以只要你使用单个参数重载)。

#1


9  

I usually use the other format of LINQ queries, like this:

我通常使用其他格式的LINQ查询,如下所示:

using (SepasProjectEntities sp = new SepasProjectEntities())
{
    var query = sp.HISAccidentLocationmappings
                    .Where(p => p.Name != null
                        && p.Name
                            .Replace("y", "x")
                            .Contains(txt1.Text))
                    .ToList();
}

Replace(char, char) won't work but Replace(string, string) will. Contains(string) should also work.

替换(char,char)将不起作用,但Replace(字符串,字符串)将。包含(字符串)也应该有效。

#2


11  

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

基于这篇包含Entity Framework支持的方法列表的MSDN文章 - 只支持一个Replace方法的重载,它是

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

我没有尝试过,但根据文档它应该工作

#3


1  

Can you flip it around? By that, I'm asking if you can do the replace on the txt1.Text value (and store it in a local variable), then compare it to the value in the database (I'm pretty sure String.Contains IS supported so long as you use the single argument overload).

你能翻转一下吗?通过这个,我问你是否可以对txt1.Text值进行替换(并将其存储在局部变量中),然后将其与数据库中的值进行比较(我非常确定String.Contains是否支持所以只要你使用单个参数重载)。