Using contains() in LINQ to SQL

后端 未结 7 1752
野性不改
野性不改 2020-11-27 05:39

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

相关标签:
7条回答
  • 2020-11-27 06:22

    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);
    

    There is a more extensive guide on the projects GitHub page: https://github.com/ninjanye/SearchExtensions

    Hope this helps future visitors

    0 讨论(0)
提交回复
热议问题