1
IQueryable<MyModel> myList = GetListMyModel().AsQueryable();
myList = myList.Where(m => m.Name.Contains('John'));

I want to return everything with Name = 'John' or 'john' or 'JOHN' (case-insensitive)

J - C Sharper
  • 1,567
  • 7
  • 23
  • 36
  • `'John'` is going to throw an error because apostrophes are for character literals. You'll want to use `"John"` instead. – Abion47 May 16 '17 at 22:05

2 Answers2

0

You can convert all into lower case filter like

IQueryable<MyModel> myList = GetListMyModel().AsQueryable();
myList = myList.Where(m => m.Name.ToLower().Contains("John".ToLower());

Or use

IQueryable<MyModel> myList = GetListMyModel().AsQueryable();
myList = myList.Where(m => m.Name.IndexOf("John",StringComparison.OrdinalIgnoreCase) != -1);
Tony Dong
  • 3,213
  • 1
  • 29
  • 32
0
myList = myList.Where(m => m.Name.ToLowerInvariant().Contains("John".ToLowerInvariant()));
earloc
  • 2,070
  • 12
  • 20
  • 1
    unless you are dealing with e.g. EntityFramework, then this approach would not succeed, only as long as you are dealing with Linq2Objects – earloc May 16 '17 at 22:05
  • @Abion47: Yepp, silly copy paste error ;) edit on the go :) – earloc May 16 '17 at 22:06
  • @J C Sharper: Please clarify the actual source beneath GetListMyModel, as the "duplicate-flag" on this question might be wrong – earloc May 16 '17 at 22:09
  • I doubt it. The crux of this question does seem to stem from a case-insensitive `Contains` check, which would make the dupe flag accurate. – Abion47 May 17 '17 at 00:45