Nested loops through recursion with usable iterators
Help! I need to generate a query with loops,but there must be undefinitly numbers of loop or much as many a client wants. I know that it can be done with recursion,but don't know exectly how.One more thing. Notice,ple开发者_StackOverflowase,that I need to use those k,i,j iterators later in "if" condition and I don't know how to catch them. Thanks
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];
}
}
}
}
}
}
I think you mean something like this.
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);
}
精彩评论