嵌套循环通过递归使用可用的迭代器

时间:2010-02-09 11:50:55

标签: c#

帮助!我需要生成一个带循环的查询,但必须有无限数量的循环或者客户想要的多少。我知道它可以通过递归来完成,但不知道如何完成。还有一件事。请注意,我需要在“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 个答案:

答案 0 :(得分: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);
}