【发布时间】:2011-08-19 00:04:40
【问题描述】:
假设我有一种特殊的方式来确定某些字符串是否“匹配”,如下所示:
public bool stringsMatch(string searchFor, string searchIn)
{
if (string.IsNullOrEmpty(searchFor))
{
return true;
}
return searchIn != null &&
(searchIn.Trim().ToLower().StartsWith(searchFor.Trim().ToLower()) ||
searchIn.Contains(" " + searchFor));
}
我想使用 Linq To Entities 和这个助手从数据库中提取匹配项。但是,当我尝试这样做时:
IQueryable<Blah> blahs = query.Where(b => stringsMatch(searchText, b.Name);
我收到“LINQ to Entities 无法识别该方法...”
如果我将代码重写为:
IQueryable<Blah> blahs = query.Where(b =>
string.IsNullOrEmpty(searchText) ||
(b.Name != null &&
(b.Name.Trim().ToLower().StartsWith(searchText.Trim().ToLower()) ||
b.Name.Contains(" " + searchText)));
这在逻辑上是等价的,然后一切正常。问题是代码不那么可读,我必须为每个我想匹配的不同实体重新编写它。
据我从this one 之类的问题中可以看出,目前我想做的事情是不可能的,但我希望我错过了什么,不是吗?
【问题讨论】:
标签: c# entity-framework linq-to-entities