[英]Dynamic search using Entity Framework word by word
I'm new to Entity Framework. 我是实体框架的新手。 I need to search this word 'johnny' in text stored in the database. 我需要在数据库中存储的文本中搜索“约翰尼”一词。 The text is from pdf files. 文本来自pdf文件。 So there are many words in the text columns. 因此,在文本列中有很多单词。
Here is my code 这是我的代码
using (var d = new DatabaseContext())
{
var l = d.Pages.Where(x => x.Text.ToLower().Contains(text.ToLower())).ToList();
}
So far the code is working. 到目前为止,代码正在运行。
But the requirement changed. 但是要求改变了。 If the user types in jhonny bravo
, the program will have to search for the word jhonny
and bravo
in the Text
column. 如果用户键入jhonny bravo
,则程序将必须在“ Text
列中搜索单词jhonny
和bravo
。 The jhonny
and bravo
should be found, even if the words in the Text
column are: 即使在“ Text
列中的单词是,也应该找到jhonny
和bravo
:
Jhonny is bravo
jhonny is not bravo
How can I solve this? 我该如何解决?
I came up with the idea that split the text and search for each word 我想到了拆分文本并搜索每个单词的想法
using (var d = new DatabaseContext())
{
var split = Regex.Split(text, @"\s+");
if (split.Count() > 1)
{
var l = d.Pages.Where(x => x.Text.ToLower().Contains(text.ToLower())).ToList();
}
}
But using the code above. 但是使用上面的代码。 How can I create a dynamic search? 如何创建动态搜索? What if the search term contains 6 words? 如果搜索词包含6个单词怎么办? How can I do the query? 我该如何查询? Thank you. 谢谢。
You can create Where
chain from word
conditions: 您可以根据word
条件创建Where
链:
using (var db = new DatabaseContext())
{
var words = Regex.Split(text, @"\s+");
var query = db.Pages.AsQuerable();
foreach(var word in words)
query = query.Where(x => x.Text.ToLower().Contains(word.ToLower()));
var answer = query.ToList();
}
Here I split the text on spaces, we then get a list of every word in the text. 在这里,我将文本分割为空格,然后获得文本中每个单词的列表。
I also use the method Distinct()
to remove all the duplicated word, I'm not sure if there is any performance gain but if you don't like it you can remove it. 我还使用方法Distinct()
删除所有重复的单词,我不确定是否会提高性能,但是如果您不喜欢它,则可以删除它。
var keywords = ["john", "bravo", "hello"]
var l = d.Pages
.Where(page => {
var words = page.Text.ToLower().Split(' '). Distinct();
foreach(var keyword in keywords) {
if (!words.Contains(keyword.ToLower())
return false;
}
return true;
}
)
.ToList();
// "john," "johnnxx" will also count as true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.