1

I am going to write search query. In that query I have a list of string like this:

var listWords=new List<string>(){"Hello","Home","dog","what"};

and also I have list of customers. How Can I search if customer's Name contains at least one of items in listWords:

  • Jack Home

  • Hot dog

  • what a big dog

Tried:

var prodList = events.Where(x =>listWords.IndexOf(x.Name.Trim().ToLower()) != -1).ToList();
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Z Daneshi
  • 57
  • 9

1 Answers1

4

Use .Where and .Any:

var result = events.Where(c => listWords.Any(w => c.Name.Contains(w)));

Problem with your solution is that you are converting your string to lower case but the collection of words has characters in upper case. In that case it will not find a match: How can I make Array.Contains case-insensitive on a string array?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • @Gilad_Green ,I dont have any problem with cases, My question is about words combinations an list. I have "Home " in listWords but dont have "Jack Home", so wont return any result. – Z Daneshi Aug 21 '17 at 06:32