How to use LINQ to SQL to create ranked search results?

Posted by quakkels on Stack Overflow See other posts from Stack Overflow or by quakkels
Published on 2011-01-03T16:01:12Z Indexed on 2011/01/03 16:53 UTC
Read the original article Hit count: 192

I am looking for a way to use l2s to return ranked result based on keywords.

I would like to take a keyword and be able to search the table for that keyword using .contains(). The trick that I haven't been able to figure out is how to get a count of how many times that keyqord appears, and then .OrderByDescending() based on that count.

So if i had some thing like:

string keyword = "SomeKeyword";

IQueryable<Article> searchResults = from a in GenesisRepository.Article
                                    where a.Body.Contains(keyword)
                                    select a;

What is the best way to order searchResults based on the number of times keyword appears in a.Body?

Thanks for any help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql