如何在LINQ to Entities中使用SQL'LIKE'?

esa*_*sac 29 c# sql linq-to-entities

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

Joh*
*Johnson
*mit*
*ack*on
Run Code Online (Sandbox Code Playgroud)

在使用LINQ to Entities之前,我有一个存储过程,它将该字符串作为参数并执行:

SELECT * FROM Table WHERE Name LIKE @searchTerm
Run Code Online (Sandbox Code Playgroud)

然后我会在传入之前执行String.Replace('*','%').

现在有了LINQ to Entities,我正在努力完成同样的事情.我知道有StartsWith,EndsWith和Contains支持,但它不会以我需要的方式支持它.

我读到了"SqlMethods.Like"并尝试了这个:

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

但是我得到以下异常:

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.
Run Code Online (Sandbox Code Playgroud)

如何使用LINQ to Entities获得相同的功能?

Yur*_*nko 34

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));
Run Code Online (Sandbox Code Playgroud)

  • 获取错误"对于需要2个参数的方法没有重载".有什么想法吗? (15认同)

mli*_*man 12

如何让它无缝地工作:

在您的EDMX模型中,添加:

    <Function Name="String_Like" ReturnType="Edm.Boolean">
      <Parameter Name="searchingIn" Type="Edm.String" />
      <Parameter Name="lookingFor" Type="Edm.String" />
      <DefiningExpression>
        searchingIn LIKE lookingFor
      </DefiningExpression>
    </Function>
Run Code Online (Sandbox Code Playgroud)

在开始的部分之后:

<edmx:ConceptualModels> <Schema Namespace="Your.Namespace"...

然后,在代码中的任何位置添加此扩展方法:

    //prior to EF 6 [System.Data.Objects.DataClasses.EdmFunction("Your.Namespace", "String_Like")]

    //With EF 6
    [System.Data.Entity.DbFunction("Your.Namespace", "String_Like")]
    public static bool Like(this string input, string pattern)
    {
        /* Turn "off" all regular expression related syntax in
         * the pattern string. */
        pattern = Regex.Escape(pattern);

        /* Replace the SQL LIKE wildcard metacharacters with the
         * equivalent regular expression metacharacters. */
        pattern = pattern.Replace("%", ".*?").Replace("_", ".");

        /* The previous call to Regex.Escape actually turned off
         * too many metacharacters, i.e. those which are recognized by
         * both the regular expression engine and the SQL LIKE
         * statement ([...] and [^...]). Those metacharacters have
         * to be manually unescaped here. */
        pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");

        return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
    }
Run Code Online (Sandbox Code Playgroud)

你有它.

现在你可以这样做:

(from e in Entities
 where e.Name like '%dfghj%'
 select e)
Run Code Online (Sandbox Code Playgroud)

要么

string [] test = {"Sydney", "Melbourne", "adelaide", "ryde"};

test.Where(t=> t.Like("%yd%e%")).Dump();
Run Code Online (Sandbox Code Playgroud)


Cra*_*ntz 8

那么,你的选择是: