Linq search any words in a term

I have a requirement to search an IQueryable list of words in a search expression.

I am currently working, but for an exact match.

list.Where(x => x.MyList.Any(y => y.ToSearch.ToLower().Contains(searchTerm.ToLower())));

      

I need someone to search for search results, it should be:

"search terms" "other search query"

I'm not sure what the best way to do this in linq can anyone help?

+3


source to share


1 answer


//split the search terms by space
var searchWords = searchTerm.ToLower().Split( " ".ToCharArray(), 
                          StringSplitOptions.RemoveEmptyEntries);

//check if any of those search terms is present
list.Where(x => x.MyList.Any(y => 
        searchWords.All(sw=>y.ToSearch.ToLower().Contains(sw))));

      



+6


source







All Articles