如何在C#中以搜索形式实现LIKE操作?

时间:2009-12-22 10:18:44

标签: c# linq

我正在使用下面的代码作为我的搜索逻辑,基本上,当相应的文本框或下拉列表上有输入时,它会评估一个字段,我的问题是代码仅用于精确匹配,最佳实现方式是什么还有.Contains()搜索,或者实现SQL LIKE搜索的搜索?

private void btnSearch_Click(object sender, EventArgs e)
    {            
        bool ok_username = !txtUsername.IsBlank();
        bool ok_firstname = !txtFirstname.IsBlank();
        bool ok_lastname = !txtLastName.IsBlank();
        bool ok_userlevels = cboUserLevels.IsItemInList();

        _query = from _v
                 in Classes.Data.getdb().vUsers
                 where
                   _v.username ==(ok_username ? txtUsername.Text : _v.username) &&
                   _v.firstname == (ok_firstname ? txtFirstname.Text : _v.firstname) &&
                   _v.lastname == (ok_lastname ? txtLastName.Text : _v.lastname) &&
                   _v.userlevel == (ok_userlevels ? cboUserLevels.Text : _v.userlevel)
                 select _v;
        gv.DataSource = _query ;


    }

当我尝试将eval表达式包含在.Contains()函数中时,它表示只有在客户端上可以计算的表达式才能用作.Contains函数中的参数。谢谢!感谢任何建议!


好的,

感谢Alex给我一个关于SQLMethods函数的想法。但是现在我会考虑桑德关于表达树链接的建议。如果有人能让这段代码更短,我会非常感激,因为在这个解决方案中,我有两组查询,一组用于灵活,另一组用于精确搜索。再次感谢Alex和Sander!

private void btnSearch_Click(object sender, EventArgs e)
    {            

        bool ok_username = !txtUsername.IsBlank();
        bool ok_firstname = !txtFirstname.IsBlank();
        bool ok_lastname = !txtLastName.IsBlank();
        bool ok_userlevels = cboUserLevels.IsItemInList();

        if (optMode.CheckedIndex == 0) //flexible search, the else part is the exact search 
        {
            _query = (from _v
                      in Classes.Data.getdb().vUsers
                      where
                      _v.userlevel == (ok_userlevels ? cboUserLevels.Text : _v.userlevel)
                      select _v);

            if (ok_username)
                _query = _query.Where(x => x.username.Contains(txtUsername.Text));

            if (ok_firstname)
                _query = _query.Where(x => x.firstname.Contains(txtFirstname.Text));

            if (ok_lastname)
                _query = _query.Where(x => x.lastname.Contains(txtLastName.Text));
        }
        else
        {
            _query = (from _v
                      in Classes.Data.getdb().vUsers
                      where
                       _v.username == (ok_username ? txtUsername.Text : _v.username) &&
                       _v.firstname == (ok_firstname ? txtFirstname.Text : _v.firstname) &&
                       _v.lastname == (ok_lastname ? txtLastName.Text : _v.lastname) &&
                       _v.userlevel == (ok_userlevels ? cboUserLevels.Text : _v.userlevel)
                      select _v);
        }

        gv.DataSource = _query.ToList();



    }

2 个答案:

答案 0 :(得分:2)

您必须从数据库中的Contains调用String

_query = 
   from _v in Classes.Data.getdb().vUsers
   where
     (txtUsername.IsBlank() || _v.username.Contains(txtUsername.Text)) &&
     (txtFirstname.IsBlank() || _v.firstname.Contains(txtFirstname.Text)) &&
     (txtLastName.IsBlank() || _v.lastname.Contains(txtLastName.Text)) &&
     (!cboUserLevels.IsItemInList() || _v.userlevel.Contains(cboUserLevels.Text))
   select _v;

如果要使用SQL Like运算符,可以使用System.Linq.Data.SqlClient.SqlMethods

_query = 
   from _v in Classes.Data.getdb().vUsers
   where
     SqlMethods.Like(_v.username, "%" + txtUsername.Text + "%") &&
     SqlMethods.Like(_v.firstname, "%" + txtFirstname.Text + "%") &&
     SqlMethods.Like(_v.lastname, "%" + txtLastName.Text + "%") &&
     SqlMethods.Like(_v.userlevel, "%" + cboUserLevels.Text + "%")
   select _v;

答案 1 :(得分:0)

您可以像这样逐步使查询更具体:

var _query = from _v in Classes.Data.getdb().vUsers
             select v;

if(!txtUsername.IsBlank())
    _query = _query.Where(x => x.username.Contains(txtUsername.Text));

if(!txtFirstName.IsBlank())
    _query = _query.Where(x => x.firstname.Contains(txtFirstName.Text));

// etc.

gv.DataSource = _query;