2010-02-09 106 views
0

幫助!我需要用循環生成一個查詢,但是必須有無限循環的數目或者客戶想要的數量。我知道它可以用遞歸完成,但不知道如何。還有一件事。請注意,我需要在後面的「if」條件中使用這些k,i,j迭代器,並且我不知道如何捕捉它們。謝謝通過遞歸與可用迭代器進行嵌套循環

class Class1 
    { 
     public void ListQuery() 
     { 
      for (int k = 0; k < listbox1.Count; k++) 
      { 
       for (int i = 0; i < listbox2.Count; i++) 
       { 
        for (int j = 0; j < listbox3.Count; j++) 
        { 
         if (k == listbox1.count-1 && i == listbox2.count-1 && j == listbox3.count-1) 
         { 
          if (!checkbox1) Query += resultset.element1 + "=" + listbox1[k]; 
          if (!checkbox2) Query += resultset.element2 + "=" + listbox1[i]; 
          if (!checkbox3) Query += resultset.element3 + "=" + listbox1[j]; 
         } 
        } 
       } 
      } 
     } 
    } 
+1

'k == listbox1'是什麼意思?有沒有'.SelectedIndex'丟失? – dtb

+0

此代碼有缺陷。 K是一個整數,listbox1不是整數,k == listbox1永遠不會是true。 – Carra

+0

此外 - 複選框來自哪裏,是否正確,listbox1用於所有三個作業的右側? –

回答

0

我認爲你的意思是這樣的。

public void BuildListBoxes() 
{ 
    this.MyCurrentListBoxes = new List<ListBox>(); 

    for (int i = 0; i < this.HowManyListBoxesMyCustomerWants; i++) 
    { 
     ListBox lbx = new ListBox() { ... }; 
     this.MyCurrentListBoxes.Add(lbx); 
     this.Controls.Add(lbx); 
    } 
} 

public void BuildQuery() 
{ 
    List<string> queryParts = new List<string>(); 

    foreach (ListBox lbx in this.MyCurrentListBoxes) 
    { 
     if (this.IsCheckBoxCheckedFor(lbx)) 
     { 
      queryParts.Add(this.GetFieldNameFor(lbx) + "=" + lbx.SelectedValue); 
     } 
    } 

    string query = String.Join(" AND ", queryParts.ToArray()); 

    this.ExecuteQuery(query); 
} 
+0

是的,多數民衆贊成在什麼。我試圖讓更輕鬆。事實上,它的唯一算法。 – user269414