相关疑难解决方法(0)

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

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

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获得相同的功能?

c# sql linq-to-entities

29
推荐指数
3
解决办法
4万
查看次数

标签 统计

c# ×1

linq-to-entities ×1

sql ×1