首页 新闻 赞助 找找看

一个关于递归调用的问题~高分求解答

0
悬赏园豆:60 [已解决问题] 解决于 2011-08-23 08:56

递归一个组织机构表里的所有组织机构,将组织机构ID拼接成组织机构串。我在调试时,在拼接完成后,return 了,又跑回来执行了3次 “GetOrgPath(orgBll.GetParentOrg(orgid).OrgId, orgFullPath);” 导致结果不是我想要的。没想明白,求解答,代码见下:

public string GetOrgPath(int orgid,string orgpath)
{
string orgFullPath = String.Empty;
orgFullPath
= orgFullPath + orgpath;

if (orgBll.GetModel(orgid).ParentOrgId != 0)
{
orgFullPath
= orgFullPath + orgBll.GetParentOrg(orgid).OrgId.ToString()+"/";
GetOrgPath(orgBll.GetParentOrg(orgid).OrgId, orgFullPath);

}
return orgFullPath;
}

asheng的主页 asheng | 初学一级 | 园豆:165
提问于:2011-08-19 15:05
< >
分享
最佳答案
0

你想拼成一个串儿,那么你这个结果比如说是string类型,那么你还需要一个参数,然后传引用。

收获园豆:60
顾晓北 | 专家六级 |园豆:10844 | 2011-08-19 17:39
麻烦写详细一些~呵呵,没听太明白。多谢
asheng | 园豆:165 (初学一级) | 2011-08-20 18:17
你需要一个ref string类型的参数,用来存你要拼接成的结果串儿。有机会找找我以前的代码,现在没时间。
顾晓北 | 园豆:10844 (专家六级) | 2011-08-20 23:39
public string GetAllCategoryById(string id)
{
string ids = id.ToString();
List categorys = GetAllCategoryByClientID("");
var list = from c in categorys
where c.ParentCatalogID == id
select c;
if (list==null)
{
return ids;
}
else
{
ids += ",";
foreach (var item in list)
{
ids += item.CatalogID;
ids += ",";
AddCategory(categorys, item.CatalogID, ref ids);
}
}
return ids;
}
#endregion

#region 把类别加到字符串上
void AddCategory(List categorys, string id, ref string ids)
{
var list = from c in categorys
where c.ParentCatalogID == id
select c;
if (list==null)
{
return;
}
else
{
//list.Count(c => c.ParentCatalogID == "0");
foreach (var item in list)
{
ids += item.CatalogID;
ids += ",";
AddCategory(categorys, item.CatalogID,ref ids);
}
}

}
#endregion
类似这样,看看吧。
顾晓北 | 园豆:10844 (专家六级) | 2011-08-25 21:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册