-3

I'm trying to get an a case-insensitive search to work in C#. Currently my code is:

private void txtSearch_KeyPress_1(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
    {
         if (!string.IsNullOrEmpty(txtSearch.Text))
         {
              var query = from o in App.Phonebook
                          where (o.PhoneNumber.Contains(txtSearch.Text) || o.Department.Contains(txtSearch.Text) || o.Name.Contains(txtSearch.Text) || o.Email.Contains(txtSearch.Text))
                          select o;
              dataGridView.DataSource = query.ToList();
         }
         else
              dataGridView.DataSource = phonebookBindingSource;
    }
}

I have tried where (o.PhoneNumber.Contains(txtSearch.Text, StringComparison.InvariantCultureIgnoreCase), but I get the error "No overload for method 'Contains' takes 2 arguments." I don't want to do ToUpper() and ToLower().

Any advice would be appreciated.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
Luc
  • 13
  • 3
  • 3
    Does this answer your question? [Case insensitive 'Contains(string)'](https://stackoverflow.com/questions/444798/case-insensitive-containsstring) – Jeremy Caney Jan 27 '20 at 00:22
  • 3
    You can just add an extension method, as [the documentation for `Contains` suggests](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains) suggests and provides an example of: _” To determine whether a string contains a specified substring by using something other than ordinal comparison (such as culture-sensitive comparison, or ordinal case-insensitive comparison), you can create a custom method. The following example illustrates one such approach.”_ – stuartd Jan 27 '20 at 00:23
  • 1
    @stuartd and hope that `App.Phonebook` does not make it a LINQ-to-SQL context. (nitpick disclaimer: I don't want to guess, and OP have not said a word on that) – quetzalcoatl Jan 27 '20 at 00:26
  • @quetzalcoatl great point. I should not try and comment from my phone – stuartd Jan 27 '20 at 00:28
  • It's worth noting that while @Luc appears to be using **ASP.NET Framework** for a **Win Forms** application, there is actually [a built-in overload for this](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netcore-3.1#System_String_Contains_System_Char_System_StringComparison_) for anyone using **ASP.NET Core 2.1+** or **.NET Standard 2.1**. – Jeremy Caney Jan 27 '20 at 00:29
  • @JeremyCaney the link you posted checks if a `char` is contained in the string, not a substring – stuartd Jan 27 '20 at 11:25
  • @stuartd: Oops. I meant to include a link to the page, not to that specific overload. A bit [further down the page](https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netcore-3.1#System_String_Contains_System_String_System_StringComparison_), there is also an overload for `Contains(string, StringComparison)` – Jeremy Caney Jan 27 '20 at 20:36

1 Answers1

0

There are various approaches.

    private void txtSearch_KeyPress_1(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)13)
        {
            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                var query = from o in App.Phonebook
                            where ((o.PhoneNumber.IndexOf(txtSearch.Text, StringComparison.InvariantCultureIgnoreCase) >= 0) || (o.Department.IndexOf(txtSearch.Text, StringComparison.InvariantCultureIgnoreCase) >= 0) || (o.Name.IndexOf(txtSearch.Text, StringComparison.InvariantCultureIgnoreCase) >= 0) || (o.Email.IndexOf(txtSearch.Text, StringComparison.InvariantCultureIgnoreCase) >= 0))
                            select o;
                dataGridView.DataSource = query.ToList();
            }
            else
                dataGridView.DataSource = phonebookBindingSource;
        }
    }

Alternative you could write an extension method:

public static class MyExtensions
{
    public static bool ContainsInsensitive(this String str, string txt)
    {
        return str.IndexOf(txt, StringComparison.InvariantCultureIgnoreCase) >= 0;
    }
}

private void txtSearch_KeyPress_1(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
    {
        if (!string.IsNullOrEmpty(txtSearch.Text))
        {
            var query = from o in App.Phonebook
                        where (o.PhoneNumber.ContainsInsensitive(txtSearch.Text) || o.Department.ContainsInsensitive(txtSearch.Text) || o.Name.ContainsInsensitive(txtSearch.Text) || o.Email.ContainsInsensitive(txtSearch.Text))
                            select o;
            dataGridView.DataSource = query.ToList();
        }
        else
            dataGridView.DataSource = phonebookBindingSource;
    }
}
InputOutput
  • 61
  • 1
  • 5