[英]String.Replace in LINQ to Entities
我有一个Asp.Net MVC 5网站,我想使用LINQ搜索实体。 目前我有一个工作搜索功能。 但是,我想在运行搜索之前添加一个替换字符串中字符的功能。 这是因为在波斯语中,单个字符有两个相似的表示,我想对它们进行搜索。
工作代码是这个(非常简化的版本):
var model = db.Restaurants.Where(r => r.Name.ToUpper().Contains(query));
我想要做的是:
query = query.Replace('آ', 'ا'); //couple of other fixes too...
var model = db.Restaurants.Where(r => r.Name.ToUpper().Replace('آ', 'ا').Contains(query));
显然,这给了我错误:
LINQ to Entities does not recognize the method 'System.String Replace(Char, Char)' method, and this method cannot be translated into a store expression
目前我唯一想到的就是将替换后的字符串存储到数据库中并查询这些字符串。 在我看来,这不是一个干净的方法。 另一个选择是在代码中运行查询(逐个查询Restaurant
),这根本没有效率。 缓存这些值会有所帮助,但我认为还有更好的方法。 这就是为什么我问这个问题,看看有没有办法将这个查询转移到数据库。
在Entity Framework 6中,您可以使用命令拦截器。 这听起来很复杂,但它们让它变得如此简单。
首先创建一个实现System.Data.Entity.Infrastructure.Interception.IDbCommandInterceptor
的类。 只有一个实现的方法很重要,其他方法只能是存根:
public class MyCommandInterceptor : IDbCommandInterceptor
{
public void ReaderExecuting(DbCommand command,
DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
command.CommandText = command.CommandText.Replace("a", "b");
}
public void NonQueryExecuting(DbCommand command,
DbCommandInterceptionContext<int> interceptionContext)
{ }
... lots of stubs
}
并通过调用激活拦截器
DbInterception.Add(new MyCommandInterceptor());
在应用程序初始化的某个地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.