1

I have a Lambda expression that search the column after submitting a form. It does work when I submit it but its doesn't search the right way I would like to search. I would like to make it work the same way it search in SQL like statement.

select * FROM tableSearch where subject like '%f5%'

This way even if 'F' is capital it still finds it. Can this be possible using Lambda expression. With the below code it only finds it if 'F' is not capital unless i enter 'F5' in subject.

if (!string.IsNullOrEmpty(searchControl.subject))
     {
        searchList = searchList.Where(x => x.subject.Contains(searchControl.subject)).ToList();
     }
anatp_123
  • 1,165
  • 2
  • 10
  • 25

2 Answers2

1

why not make them both ToLower:

searchList.Where(x => x.subject.ToLower().Contains(searchControl.subject.ToLower()))

or:

searchList.Where(x => x.subject.IndexOf(searchControl.subject, StringComparison.OrdinalIgnoreCase) >= 0)
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

You need to use StringComparison as below:

 searchList = searchList.Where(x => x.subject.Contains(searchControl.subject,StringComparison.CurrentCultureIgnoreCase)).ToList();

By using that you ignore case sensitivity.

Nick Mehrdad Babaki
  • 11,560
  • 15
  • 45
  • 70
  • For this option i get error message 'Nooverload for method 'Contains' takes 2 arguments – anatp_123 Dec 04 '18 at 22:38
  • @anatp_123 you can do `searchList.Where(x => x.subject.IndexOf(searchControl.subject, StringComparison.OrdinalIgnoreCase) >= 0)` instead. – Ousmane D. Dec 04 '18 at 22:43