0

I'm using the following line of code to match search word in my data list. But Contains is case-sensitive if I search for "search" it doesn't match with "Search" or "seArch".
What are my alternatives to Contains? How Can I replace it with IndexOf?

IEnumerable<Compound> allCompounds = _context.Compounds.ToList();
IEnumerable<Compound> filteredCompounds;
if (!string.IsNullOrEmpty(param.sSearch))
{
    filteredCompounds = allCompounds
             .Where(c => c.CompoundName.Contains(param.sSearch)
                         ||
                         c.RI.Contains(param.sSearch)
                          ||
                         c.MolecularWeight.Contains(param.sSearch)
                         ||
                         c.MolecularFormula.Contains(param.sSearch)
                         );
}
  • 1
    See [this Microsoft documentation](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netframework-4.7.2) for an example of a `Contains()` extension method that allows you to ignore case. (Scroll down to the *Remarks* section.) – Matthew Watson Aug 27 '18 at 09:01

2 Answers2

0

Maybe this can help you solve the problem.

        IEnumerable<Compound> allCompounds = _context.Compounds.ToList();
        IEnumerable<Compound> filteredCompounds;
        if (!string.IsNullOrEmpty(param.sSearch))
        {
            filteredCompounds = allCompounds
                     .Where(c => c.CompoundName.ToLower().Contains(param.sSearch.ToLower())
                                 ||
                                 c.RI.ToLower().Contains(param.sSearch.ToLower())
                                  ||
                                 c.MolecularWeight.ToLower().Contains(param.sSearch.ToLower())
                                 ||
                                 c.MolecularFormula.ToLower().Contains(param.sSearch.ToLower())
                                 );
        }
StalkeR
  • 13
  • 3
0

Try to use param.sSearch.ToLower() replace it every where you have used the param.sSearch.. it will work