【发布时间】:2010-06-22 18:04:32
【问题描述】:
我有一个文本框,允许用户指定搜索字符串,包括通配符,例如:
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 获得相同的功能?
【问题讨论】:
标签: c# sql linq-to-entities