1

I have the following code

query = query.Where(x => words.Any(x.Message.Contains));

words is a string[] and x.Message is a string

I would like to filter out my query based on all the words in my array but I would like this not to be case sensitive comparison so if i type 'bob' or 'BOb' it should not care and still compare those words against the message if Message is 'BOB is awesome' or 'bob is awesome'

Zoinky
  • 4,083
  • 11
  • 40
  • 78

2 Answers2

3

A better option is to use Contains overload with StringComparison parameter:

query = query.Where(x => words.Any(s => x.Message
    .Contains(s, StringComparison.InvariantCultureIgnoreCase)));
Roman Ryzhiy
  • 1,540
  • 8
  • 5
1

This will compare the list of strings to the message as you desire. I use .ToLower() to convert both the string and each word in the list so it's essentially case-insensitive.

query = query.Where(x => words.Any(s => x.Message.ToLower().Contains(s.ToLower())));

Check if a string contains an element from a list (of strings): Check if a string contains an element from a list (of strings)

Tim Jarosz
  • 1,133
  • 1
  • 8
  • 15