【发布时间】:2015-01-27 02:51:49
【问题描述】:
我正在使用安装位置的树形结构:每个位置都可能包含子 InstallationPlace,这些也可以包含子 InstallationPlace,依此类推。我有以下功能:
public JsonResult GetInstPlacesTree()
{
InstallationPlaceModel ipm = new InstallationPlaceModel();
var dataContext = ipm.getRootInstallationPlaces();
var instPlaces = from ip in dataContext.installationPlaces
select new
{
id = ip.installationPlace.id,
Name = ip.installationPlace.mediumDescription,
};
return Json(instPlaces, JsonRequestBehavior.AllowGet);
}
这个函数只返回树的根层。
我有两种工作方法:
- 一个返回根安装位置;
- 另一个返回给定安装位置的子代;
它们都返回 IEnumerable 变量。
getRootInstallationPlaces();
getChildInstallationPlaces(id);
我怎样才能调用所有的安装位置和相应的孩子?
我已经尝试过GetInstPlacesTree() 函数的替代方法:
private IEnumerable<TreeViewItemModel> GetDefaultInlineData()
{
InstallationPlaceModel ipm = new InstallationPlaceModel();
List<TreeViewItemModel> fullTree = new List<TreeViewItemModel>();
var gipo = ipm.getChildInstallationPlaces(currentInstallationPlace.InstallationPlaceId);
List<TreeViewItemModel> childTree = new List<TreeViewItemModel>();
if (gipo.installationPlaces.Count() > 0)
{
foreach (wsInstallationPlace.installationPlaceOutput child in gipo.installationPlaces)
{
TreeViewItemModel childTreeItem = new TreeViewItemModel
{
Text = child.installationPlace.mediumDescription,
Id = child.installationPlace.id
};
childTree.Add(childTreeItem);
}
}
TreeViewItemModel fatherTreeItem = new TreeViewItemModel
{
Text = currentInstallationPlace.InstallationPlaceMediumDescription,
Id = currentInstallationPlace.InstallationPlaceId,
Items = childTree
};
fullTree.Add(fatherTreeItem);
return fullTree;
}
有什么帮助吗?
【问题讨论】:
-
在理解递归之前,必须先理解递归。