I want to search a database of books by several keywords. The more keywords I provide the narrower the search is to be. Here is my code:
var words = text.Split(' ');
IQueryable<Reference> query = null;
foreach (string word in words)
{
var result = from r in _dbConnection.GetTable<Reference>()
where r.Title.Contains(word)
|| r.ReferenceAuthor.Any(a => a.Person.LastName.Contains(word) || a.Person.FirstName.Contains(word))
|| r.ReferenceCategory.Any(c => c.Category.Name.Contains(word))
|| r.ReferenceKeyword.Any(k => k.Keyword.Name.Contains(word))
orderby r.Title
select r;
query = query == null ? result : query.Intersect(result);
}
query.OrderBy(r => r.Title);
The problem is, that the search does not in fact get narrower the more keywords I provide. The results even vary depending on the order in which I provide the keywords. Also, that last OrderBy() call doesn't work reliably if more than one keyword is involved. Is my idea flawed, or the way I implement it?