<分区>
我有一个文本框,允许用户指定搜索字符串,包括通配符,例如:
Joh*
*Johnson
*mit*
*ack*on
在使用 LINQ to Entities 之前,我有一个存储过程,它将该字符串作为参数并执行:
SELECT * FROM Table WHERE Name LIKE @searchTerm
然后我会在传入之前执行 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 };
但是我得到以下异常:
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 to Entities 获得相同的功能?