简体   繁体   English

如何进行忽略大小写搜索 (C#)

[英]How can I do an ignore case search (C#)

I'm trying to get an a case-insensitive search to work in C#.我正在尝试在 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() .我试过where (o.PhoneNumber.Contains(txtSearch.Text, StringComparison.InvariantCultureIgnoreCase) ,但我收到错误“方法'Contains'没有重载需要2个参数。”我不想做ToUpper()ToLower() .

Any advice would be appreciated.任何意见,将不胜感激。

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM