流畅的接口/扩展方法 - 将平面列表变成导航树
我目前有一个扩展方法,它将 Tab 类型的 IEnumerable 转换为 TabNode 的分层集合。
// If Tab has no parent its ParentId is -1
public class Tab
{
public int TabId { get; set; }
public string TabName { get; set; }
public int Level { get; set; }
public int ParentId { get; set; }
}
public class TabNode
{
public TabInfo Tab { get; set; }
public IEnumerable<TabNode> ChildNodes { get; set; }
public int Depth { get; set; }
}
例如,下面的代码将为您提供位于 TabId 32 的父级之下的 TabNode 的集合 - 最大深度级别为 4。
IEnumerable<Tab> tabs = GetTabs();
IEnumerable<TabNode> = tabs.AsNavigationHierarchy(32,4);
这很令人困惑,并且对于进一步细化不太友好。 如果我想指定某个级别而不是 ParentID 怎么办?
我想做的是这样的:
IEnumerable<TabNode> = tabs.AsNavigationHierarchy().WithStartLevel(2).WithMaxDepth(5)
我不知道如何优雅地做到这一点。 你能帮助我吗?
这是我当前的函数,由我的扩展方法调用(基于我在 www.scip.be)。
private static IEnumerable<TabNode>
CreateHierarchy(
IEnumerable<TabInfo> tabs,
int startTabId,
int maxDepth,
int depth)
{
IEnumerable<TabInfo> children;
children = tabs.Where(i => i.ParentId.Equals(startTabId));
if (children.Count() > 0)
{
depth++;
if ((depth <= maxDepth) || (maxDepth == 0))
{
foreach (var childTab in children)
yield return
new TabNode()
{
Tab = childTab,
ChildNodes =
CreateHierarchy(tabs, childTab.TabID, maxDepth, depth),
Depth = depth
};
}
}
}
I currently have an extension Method which converts an IEnumerable of type Tab into a hierarchical collection of TabNodes.
// If Tab has no parent its ParentId is -1
public class Tab
{
public int TabId { get; set; }
public string TabName { get; set; }
public int Level { get; set; }
public int ParentId { get; set; }
}
public class TabNode
{
public TabInfo Tab { get; set; }
public IEnumerable<TabNode> ChildNodes { get; set; }
public int Depth { get; set; }
}
For instance, the following would give you a collection of TabNodes who are below a Parent with TabId 32 - the maximum level of depth is 4.
IEnumerable<Tab> tabs = GetTabs();
IEnumerable<TabNode> = tabs.AsNavigationHierarchy(32,4);
This is confusing and not very friendly for further refinement. What If I'd like to specify a certain Level instead of a ParentID?
What I'd like to do is something like this:
IEnumerable<TabNode> = tabs.AsNavigationHierarchy().WithStartLevel(2).WithMaxDepth(5)
I'm stuck as how to do this elegantly. Can you help me?
This is my current function which is called by my extension methods (based on an article I've found on www.scip.be).
private static IEnumerable<TabNode>
CreateHierarchy(
IEnumerable<TabInfo> tabs,
int startTabId,
int maxDepth,
int depth)
{
IEnumerable<TabInfo> children;
children = tabs.Where(i => i.ParentId.Equals(startTabId));
if (children.Count() > 0)
{
depth++;
if ((depth <= maxDepth) || (maxDepth == 0))
{
foreach (var childTab in children)
yield return
new TabNode()
{
Tab = childTab,
ChildNodes =
CreateHierarchy(tabs, childTab.TabID, maxDepth, depth),
Depth = depth
};
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tabs.AsNavigationHeirachy 可以返回一个 HerirchyQuery 对象,您的下一个扩展方法将期望该对象。 这将使您将它们链接在一起。
tabs.AsNavigationHeirachy could return a HerirchyQuery object which your next extension methods would then expect. This will let you chain them together.