实体框架 4.1 - 如何防止 EF 转义通配符?
有没有办法告诉 EF 不要转义我的通配符?
我当前的解决方案如下所示:
var movieNameWithProperWildcards = string.Format("%{0}%",
movieName.ToLower().Replace("*", "%"));
var sqlParameter = new SqlParameter { ParameterName = "searchParameter", Value =
movieNameWithProperWildcards };
List<Movie> movieEntities = MovieContext.Movies.SqlQuery("select * from Movies WHERE
Lower(title) like @searchParameter", sqlParameter).ToList();
但这会更好:
List<Movie> movieEntities = MovieContext.Movies.Where(movie =>
movie.Title.ToLower().Contains(movieName));
br 雷内_r
Is there a way to tell EF not to escape my wildcards?
My current solution looks like this:
var movieNameWithProperWildcards = string.Format("%{0}%",
movieName.ToLower().Replace("*", "%"));
var sqlParameter = new SqlParameter { ParameterName = "searchParameter", Value =
movieNameWithProperWildcards };
List<Movie> movieEntities = MovieContext.Movies.SqlQuery("select * from Movies WHERE
Lower(title) like @searchParameter", sqlParameter).ToList();
But that would be much nicer:
List<Movie> movieEntities = MovieContext.Movies.Where(movie =>
movie.Title.ToLower().Contains(movieName));
br
rene_r
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要使用
LIKE
,这样您就可以使用您的方式,ESQL 或 Linq-to-entities 和Contains
规范函数。 ORM 工具不负责将通配符从您的表示形式转换为数据库中的通配符表示形式 - 您必须使用 ORM 提供的工具自己完成此操作。一般来说,这应该有效:
String.ToLower
和String.Contains
都在 支持的规范函数。You want to use
LIKE
so you can use either your way, ESQL or Linq-to-entities andContains
canonical function. ORM tool is not responsible for translating wildcards from your representation to wildcards representation in the database - you must do it yourselves with tools provided by ORM.Generally this should work:
Both
String.ToLower
andString.Contains
are in the list of supported canonical functions.检查执行的查询是否正确。请参阅此处如何使用上下文的日志来执行此操作 http:// /www.thereforesystems.com/view-query-generate-by-linq-to-sql/
Check that the query that gets executed is right. See here how you can use context's Log to do that http://www.thereforesystems.com/view-query-generate-by-linq-to-sql/