Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't do what you want to do without issuing multiple queries to the database - essentially one per search term. If you are happy to do that, then here is an easy way to do it:</p> <pre><code>var terms = new [] { "s", "t", "r", "e", "b", "c", }; var ids = from term in terms from id in _db.Books .Where(book =&gt; book.Title.Contains(term)) .Select(book =&gt; book.Id) group term by id into gts orderby gts.Count() descending select gts.Key; var selectedIds = ids.Take(50).ToArray(); var query = from book in _db.Books where selectedIds.Contains(book.Id) select book; </code></pre> <p>I wrote the <code>ids</code> to return a list of ids sorted by those that match the most terms first. This was to most closely get the same kind of result that you wanted in your question. I then decided to use a <code>Take(50)</code> to get the top 50 results. You can obviously change this strategy to suit your needs, but you must end up with an array of ids to use in the final query.</p> <p>I hope this helps.</p> <hr> <p><strong>EDIT: based on OP's edit.</strong></p> <p>Here's how to query with the score included:</p> <pre><code>var terms = new [] { "s", "t", "r", "e", "b", "c", "l", "i", }; var idScores = from term in terms from id in _db.Books .Where(book =&gt; book.Title.Contains(term)) .Select(book =&gt; book.BookID) group term by id into gts select new { Id = gts.Key, Score = gts.Count(), }; var selectedIds = idScores.Select(x =&gt; x.Id).Take(50).ToArray(); var selectedBooks = from book in _db.Books where selectedIds.Contains(book.BookID) select book; var query = from b in selectedBooks.ToArray() join x in idScores on b.BookID equals x.Id orderby x.Score descending select new { Title = b.Title, Type = "Book", Link = "Books/Details/" + b.BookID, Score = x.Score, }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload