I\'m trying to implement a very basic keyword search in an application using linq-to-sql. My search terms are in an array of strings, each array item being one word, and I
Using the NinjaNye.SearchExtension nuget package allows you to perform this search with ease:
string[] terms = new[]{"search", "term", "collection"};
var result = db.Parts.Search(terms, p => p.PartName);
You could also search multiple string properties
var result = db.Parts.Search(terms, p => p.PartName, p.PartDescription);
Or perform a RankedSearch
which returns IQueryable<IRanked<T>>
which simply includes a property which shows how many times the search terms appeared:
//Perform search and rank results by the most hits
var result = db.Parts.RankedSearch(terms, p => p.PartName, p.PartDescription)
.OrderByDescending(r = r.Hits);
Hope this helps future visitors