私は現在、IEnumerable<T>
を実装するクラスを作成して、ParentIdプロパティを通じて互いに参照を持つオブジェクトのフラットなリストから階層を構築しようとしています。私はyield文については、だから、このlinqの "Select New"は評価/負荷をトリガしますか?
IEnumerable<Tab> tabs = GetTabs();
IEnumerable<TabNode> tabNodes = tabs.AsHierarchy().WithStartLevel(2).WithMaxDepth(5);
ような何かを行うことができますので、私はこのための流れるようなインターフェイスを書きたいのですが、私は私が私のNodeHierarchy : IEnumerable<TabNode>
クラス内でこのような何かを行うことができるかどうかを疑問に思う:
private IEnumerable<TabNode> _nodes;
public NodeHierarchy(IEnumerable<Tab> tabs)
{
_nodes = CreateHierarchy(tabs);
}
public IEnumerable<TabNode> CreateHierarchy(IEnumerable<Tab> tabs)
{
/* About this block: I'm trying to find the top level
nodes of the first tab collection, maybe this is done poorly? */
var tabIds = tabs.Select(t => t.TabID);
IEnumerable<TabNode> nodes = from tab in tabs
where !tabIds.Contains(tab.ParentId)
select new TabNode {
Tab = node,
ChildNodes = CreateHierarchy(tabs, node.TabID, 1),
Depth = 1 };
return nodes;
}
または私はこのような何かをしなければならないかどうか :
private IEnumerable<TabNode> _nodes;
public NodeHierarchy(IEnumerable<Tab> tabs)
{
_nodes = CreateHierarchy(tabs);
}
public IEnumerable<TabNode> CreateHierarchy(IEnumerable<Tab> tabs)
{
var tabIds = tabs.Select(t => t.TabID);
IEnumerable<Tab> startingNodes = from tab in tabs
where !tabIds.Contains(tab.ParentId)
select tab;
foreach(Tab node in startingNodes)
{
yield return
new TabNode()
{
Tab = node,
ChildNodes = CreateHierarchy(tabs, node.TabID, 1),
Depth = 1
};
}
タブの代わりにスペースを使用してコードを再フォーマットできますか?現時点では判読不能です。 –
ええ、ありがとう、私はそれに取り組んでいる...コピー貼りジョブは実際にここでは動作しませんでした:) – kitsune
今修正する必要があります... – kitsune