Click here to Skip to main content
14,328,723 members
Rate this:
Please Sign up or sign in to vote.
Hi,I want to call a recursive function using for loop,but when it calls recursively its start from beginning value , I want to pass the all values one by one ,but my code below passes the one(same) value every time..So how can i pass the all values to the function call?

protected String BottomTree(String AncestorFamilyID, ArrayList RelationPath)
    {
        ArrayList bottomMembers = new ArrayList();

        sSQL = "SELECT DISTINCT AF_MemberID FROM Family_Master";
        sSQL = sSQL + " WHERE AF_FamilyID IN(" + AncestorFamilyID + ")";

        DS2 = new DataSet();
        da2 = new OdbcDataAdapter(sSQL, conn);
        da2.Fill(DS2);

        if (DS2.Tables.Count > 0)
        {
            if (DS2.Tables["table"].Rows.Count > 0)
            {
                for (int i = 0; i < DS2.Tables["table"].Rows.Count; i++)
                {
                    bottomMembers.Add(DS2.Tables["table"].Rows[i]["AF_MemberID"].ToString());

                    if (bottomMembers[i].ToString() == lblUserToMemberID.Text)
                    {
                        return lblUserToMemberID.Text;
                    }
                    else
                    {
                        sSQL = "SELECT AF_FamilyID FROM Family_Master";
                        sSQL = sSQL + " WHERE AF_MemberID = '" + bottomMembers[i] + "'";
                        sSQL = sSQL + " AND AF_MemberType IN('H')";

                        DS3 = new DataSet();
                        da3 = new OdbcDataAdapter(sSQL, conn);
                        da3.Fill(DS3);

                        if (DS3.Tables.Count > 0)
                        {
                            if (DS3.Tables["table"].Rows.Count > 0)
                            {
                                lblUserFromFamilyID.Text = DS3.Tables["table"].Rows[0]["AF_FamilyID"].ToString();
                            }
                        }

                        return BottomTree(lblUserFromFamilyID.Text, RelationPath);
                    }
                }
            }
        }
        DS2.Dispose();
        da2.Dispose();


        return null;

    }



Any Help?
Posted
Updated 23-Feb-14 20:08pm
v2
Comments
   
Not clear at all. It needs more accurate explanation, first of all, on what you want to achieve.
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

You have to use 2 methods. First will invoke the second one and only the second will be recursive!
Here is an example by using Entity Framework:
public static int[] GetCategoyWithChildrenIDList(SLOnlineShopEntities dataContext, int categoryID)
{
    List<Category> childrenTree = GetCategoryWithChildrenTreeAsList(dataContext, categoryID);
    if (childrenTree.Count < 1)
        return new int[0];
    //
    List<int> idList = new List<int>();
    //
    foreach (Category category in childrenTree)
    {
        idList.Add(category.ID);
    }
    //
    return idList.ToArray();
}

private static List<Category> GetCategoryWithChildrenTreeAsList(SLOnlineShopEntities dataContext, int categoryID)
{
    List<Category> childrenTree = new List<Category>();
    Category mainCategory = dataContext.Categories.FirstOrDefault(c => c.ID == categoryID && (c.Inactive == false || c.Inactive == null));
    if (mainCategory == null)
        return childrenTree;
    //
    childrenTree.Add(mainCategory);
    List<Category> directChildrenList = dataContext.Categories.Where(c => c.ParentID == categoryID && (c.Inactive == false || c.Inactive == null)).ToList();
    if (directChildrenList.Count == 0)
        return childrenTree;
    //
    childrenTree.AddRange(directChildrenList);
    //
    foreach (Category category in directChildrenList)
    {
        List<Category> tempList = GetCategoryWithChildrenTreeAsList(dataContext, category.ID);
        if (tempList.Count > 0)
            childrenTree.AddRange(tempList);
    }
    //
    return childrenTree;
}
   
v4
Comments
SVT02 24-Feb-14 2:15am
   
How can i use second method as recursive? any example?
Raul Iloc 24-Feb-14 2:22am
   
I just put an example by using EF.
Rate this:
Please Sign up or sign in to vote.

Solution 2

i think ,you are looking for this

Recursive queries using Common table expression
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100