2012-05-24 13 views
1

我有一個列表的對象和id喜歡用多個參數查詢列表,以減少搜索結果的搜索頁面。Lambda或Linq方法來搜索多個參數

 int SecLink = (!string.IsNullOrEmpty(Request.QueryString["Sector"])) ? Convert.ToInt32(Request.QueryString["Sector"]) : 0; 
     int LocLink = (!string.IsNullOrEmpty(Request.QueryString["Location"])) ? Convert.ToInt32(Request.QueryString["Location"]) : 0; 
     int IndLink = (!string.IsNullOrEmpty(Request.QueryString["Industry"])) ? Convert.ToInt32(Request.QueryString["Industry"]) : 0; 
     int VacLink = (!string.IsNullOrEmpty(Request.QueryString["Vacancy"])) ? Convert.ToInt32(Request.QueryString["Vacancy"]) : 0; 

     string keyword = Request.QueryString["SearchTerm"]; 

     var dx = new DataX(); 
     var lstJobs = dx.GetAllJobs().Where(x => x.SectorLink.Equals(SecLink) && x.LocationLink.Equals(LocLink) && x.IndustryLink.Equals(IndLink) && x.VacancyTypeLink.Equals(VacLink) && x.JobName.Contains(keyword)).ToList(); 

     if (lstJobs.Count > 0) 
     { 
      uiRptSearchJobs.DataSource = lstJobs; 
      uiRptSearchJobs.DataBind(); 

      uiLitSearchResults.Text = string.Format("<h4>Search result found {0} matches</h4>", lstJobs.Count); 
     } 

由於沒有從上一頁中選擇搜索參數,所以搜索參數可能爲'0',所以結果應該反映他的。

這是查詢字符串IM傳:

Default.aspx?section=search&Sector=4&Location=0&Industry=0&Vacancy=0&SearchTerm=

,但你可以看到他們的查詢字符串將與哪些用戶從以前的頁面選擇更改。

+3

不明白你的問題/問題。 – leppie

+0

var lstJobs返回所有作業,即使LocationLink = 0,IndustryLink = 0但只有SectorLink = 4 –

+1

@NeilHodges當然,它會返回** no **作業,因爲您將測試'LocationLink == 0',這可能從來都不是真的? –

回答

7

如果我理解正確,如果參數的值爲0,您不想過濾?如果是這樣,兩種解決方案:

  1. 檢查的參數是你的條件等於0:

    var lstJobs = dx.GetAllJobs().Where(x => 
        (SecLink == 0 || x.SectorLink.Equals(SecLink)) 
        && (LocLink == 0 || x.LocationLink.Equals(LocLink)) 
        && (IndLink == 0 || x.IndustryLink.Equals(IndLink)) 
        && (VacLink == 0 || x.VacancyTypeLink.Equals(VacLink)) 
        && x.JobName.Contains(keyword)).ToList(); 
    
  2. LINQ的善良,動態構造查詢:

    var query = dx.GetAllJobs().Where(x => x.JobName.Contains(keyword)); 
    
        if (SecLink != 0) 
        { 
         query = query.Where(x => x.SectorLink.Equals(SecLink)); 
        } 
    
        if (LocLink != 0) 
        { 
         query = query.Where(x => x.LocationLink.Equals(LocLink)); 
        } 
    
        if (IndLink != 0) 
        { 
         query = query.Where(x => x.IndustryLink.Equals(IndLink)); 
        } 
    
        if (VacLink != 0) 
        { 
         query = query.Where(x => x.VacancyTypeLink.Equals(VacLink)); 
        } 
    
        var lstJobs = query.ToList(); 
    
+0

謝謝你正是我所需要的,工作很棒! –

+0

我認爲第一種方法足夠簡單/可讀(特別是如果最近花時間在SQL上),並且顯然是少了一行代碼 – mmcrae

3

一種選擇是有條件執行Where條款:

如果搜索術語應當AND組合在一起:

var lstJobs = dx.GetAllJobs(); 

if (SecLink > 0) 
    lstJobs = lstJobs.Where(x => x.SectorLink.Equals(SecLink)) 

if (LocLink > 0) 
    lstJobs = lstJobs.Where(x => x.LocationLink.Equals(LocLink)) 

if (IndLink > 0) 
    lstJobs = lstJobs.Where(x => x.IndustryLink.Equals(IndLink)) 

if (VacLink > 0) 
    lstJobs = lstJobs.Where(x => x.VacationLink.Equals(VacLink)) 

// Performance does not suffer because the query will 
// not get evaluated until it's required. For example, 
// here we call .ToList, which forces the query to be evaluated. 
var result = lstJobs.ToList(); 

但是,你清楚你所需要的搜索字詞OR連起來。在這種情況下:

var lstJobs = dx.GetAllJobs().Where(x => x.JobName.Contains(keyword)); 

if (SecLink > 0) 
    lstJobs = lstJobs.Union(
     dx.GetAllJobs().Where(x => x.SectorLink.Equals(SecLink)) 

if (LocLink > 0) 
    lstJobs = lstJobs.Union(
     dx.GetAllJobs().Where(x => x.LocationLink.Equals(LocLink)) 

etc... 
+0

問題是,如果用戶從prevoius頁面的下拉菜單中選擇多個下拉菜單,則url將會像Sector = 4&Location = 3&Industry = 2&Vacancy = 0&SearchTerm = Engineer,並且它必須顯示與所有搜索標準 –

+0

我已經更新了回覆,並大致瞭解瞭如何將您的結果放在一起。從問題中我們不清楚這個職位名稱與其他搜索條款是如何相關的,因此我認爲它與其他搜索條件一起得到了ORed。 –

+0

謝謝 - 你的建議效果不錯,感謝你花在它上面的時間,現在正在工作;) –