如何在C#中递归调用for循环中的函数 [英] How To Recursively Call a Function Inside for Loop in C#
本文介绍了如何在C#中递归调用for循环中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用for循环调用递归函数,但是当它以递归方式调用它从开始值开始时,我想逐个传递所有值,但是我的代码在下面传递了一个(相同)值时间..那么如何将所有值传递给函数调用?
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]。行[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 [ 表]。行[ 0 ] [ AF_FamilyID跨度>]的ToString();
}
}
return BottomTree(lblUserFromFamilyID.Text,RelationPath);
}
}
}
}
DS2.Dispose();
da2.Dispose();
return null ;
}
任何帮助?
解决方案
您必须使用2种方法。首先将调用第二个,只有第二个将是递归的!
以下是使用实体框架的示例:
public static int [] GetCategoyWithChildrenIDList(SLOnlineShopEntities dataContext,int categoryID)
{
List < Category > childrenTree = GetCategoryWithChildrenTreeAsList(dataContext,categoryID);
if(childrenTree.Count < 1 )
return new int [0];
//
列表< int > idList = new List < int > ();
//
foreach(childrenTree中的类别类别)
{
idList.Add(category.ID);
}
//
返回idList.ToArray();
}
私有静态列表< 类别 > GetCategoryWithChildrenTreeAsList(SLOnlineShopEntities dataContext,int categoryID)
{
List < 类别 > childrenTree =新列表< 类别 > ();
类别mainCategory = dataContext.Categories.FirstOrDefault(c => c.ID == categoryID&&(c.Inactive == false || c.Inactive == null));
if(mainCategory == null)
return childrenTree;
//
childrenTree.Add(mainCategory);
列表< 类别 > 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(directChildrenList中的类别类别)
{
List < 类别 > tempList = GetCategoryWithChildrenTreeAsList(dataContext,category.ID);
if(tempList.Count> 0)
childrenTree.AddRange(tempList);
}
//
返回childrenTree;
}
我想,你正在寻找这个
使用公用表表达式的递归查询
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?
解决方案
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; }
i think ,you are looking for this
Recursive queries using Common table expression
这篇关于如何在C#中递归调用for循环中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文