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. 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. The jhonny
and bravo
should be found, even if the words in the Text
column are:
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? How can I do the query? Thank you.
You can create Where
chain from word
conditions:
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.