我有一个记录数据库,每条记录都有一个标题。我希望能够使用将被分隔成列表或数组的搜索字符串来搜索此数据库。
例如,如果我搜索“Book Dog”,它将搜索所有标题中包含“Book”或“Dog”的书名。
我正在使用 Entity Framework ,我想写下我想做的事情的最简单方法是
string[] words;
var posts = (from p in ctx.posts
where p.title.contains(words)
select p).ToList();
我尝试使用在网上找到的 StringExtension,但我会收到以下错误 “LINQ to Entities 无法识别‘Boolean ContainsAny(System.String, System.String[])’方法,并且无法将此方法转换为存储表达式。”
扩展名是
public static bool ContainsAny(this string str, params string[] values)
{
if (!string.IsNullOrEmpty(str) || values.Length > 0)
{
foreach (string value in values)
{
if (str.Contains(value))
return true;
}
}
return false;
}
最佳答案
你在找这个吗?
var posts = (from p in ctx.posts
where words.Any(w => p.title.Contains(w))
select p).ToList();
关于asp.net - Entity Framework 用字符串列表搜索记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16245989/