How to ignore case sensitivity in StartsWith for LINQ FindAll?

WinFXGuy picture WinFXGuy · May 22, 2014 · Viewed 9.8k times · Source

I have the following code:

ContactList = ContactList.FindAll(p => p.DeptName.StartsWith(optAlpha.SelectedItem.Value)).ToList();

If DeptName="test" and optAlpha.SelectedItem.Value="T", it doesn't work.

I tried with the following code, still doesn't work.

ContactList = ContactList.FindAll(p => p.DeptName.ToLower().StartsWith(optAlpha.SelectedItem.Value.ToLower())).ToList();

Answer

Mike Perrenoud picture Mike Perrenoud · May 22, 2014

Just use

StartsWith(optAlpha.SelectedItem.Value, StringComparison.InvariantCultureIgnoreCase);

and it will ignore the case during the default comparison.