开发者

Nested loops through recursion with usable iterators

开发者 https://www.devze.com 2022-12-19 10:12 出处:网络
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

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消