2009-12-22 87 views
2

我在下面的代碼中使用了我的搜索邏輯,基本上,它在相應的文本框或下拉列表中存在輸入時評估字段,我的問題是代碼僅用於完全匹配,最好的實現方式是什麼a .Contains()搜索還是實現SQL LIKE搜索的搜索?如何在C#中的搜索表單中實現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表達,它說,僅可以在客戶端上被求值的表達式可以被用作在功能。載的參數。謝謝!感謝任何建議!


好吧,

感謝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(); 



    } 
+0

切換VB.NET並使用['Like'運算符](http://msdn.microsoft.com/en-us/library/swf8kaxw(v = vs.90).aspx);) –

回答

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; 
+0

+1這是關於查詢表達式的另一件好事。 只需要在這裏添加Contains()調用。 – Botz3000

+0

-1這並不建議如何使用'Contains'以及如何使用SQL Like操作符。 –

+0

Hello Sander, 感謝您的回答,您的回答提醒我,我們確實可以鏈接查詢表達式。將基於你和亞歷克斯的答案制定解決方案。 – thepointofwork

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; 
+0

您好亞歷克斯, 感謝您的SQLMethods提示,我不知道這一點。一個問題,當它被翻譯時,這種連接形式是否可以安全地從sql注入?我知道linq從sql注入是安全的,但如何使用這種形式?謝謝您的幫助! – thepointofwork