如何使用SQL“LIKE'与LINQ到实体? [英] How to use SQL 'LIKE' with LINQ to Entities?

查看:201
本文介绍了如何使用SQL“LIKE'与LINQ到实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,允许用户指定搜索字符串,包括通配符,例如:

I have a textbox that allows a user to specify a search string, including wild cards, for example:

Joh*
*Johnson
*mit*
*ack*on

在使用LINQ到实体,我有一个存储过程,而把这个字符串当作参数,所做的:

Before using LINQ to Entities, I had a stored procedure which took that string as parameter and did:

SELECT * FROM Table WHERE Name LIKE @searchTerm

然后,我只想做一个与string.replace('*','%')传递给它之前。

And then I would just do a String.Replace('*', '%') before passing it in.

现在使用LINQ到实体我试图完成同样的事情。我知道有StartsWith,和的endsWith包含的支持,但不会支持它,我需要的方式。

Now with LINQ to Entities I am trying to accomplish the same thing. I know there is StartsWith, EndsWith and Contains support, but it won't support it in the way that I need.

我读到SqlMethods.Like,并试图这样的:

I read about "SqlMethods.Like" and tried this:

var people = from t in entities.People
             where SqlMethods.Like(t.Name, searchTerm)
             select new { t.Name };

但是我收到以下异常:

However I am getting the following exception:

LINQ to Entities does not recognize the method 'Boolean Like(System.String, 
System.String)' method, and this method cannot be translated into a store 
expression.

我该如何获得使用LINQ到实体此相同的功能?

How would I get this same functionality using LINQ to Entities?

推荐答案

<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/6529a35b-6629-44fb-8ea4-3a44d232d6b9/\">http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/6529a35b-6629-44fb-8ea4-3a44d232d6b9/

var people = entities.People.Where("it.Name LIKE @searchTerm", new ObjectParameter("searchTerm", searchTerm));

这篇关于如何使用SQL“LIKE'与LINQ到实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆