를 사용하여 동일한 유형의 객체의 객체 트리의 깊이를 가져옵니다 그 자체의 객체가 존재 하는가?나는이 객체가 람다 식
나는 this JavaScript post을 보았지만 한 줄의 람다 명령문으로 변환하는 데 어려움을 겪고 있습니다.
는이new dtHeader(){ ParentHeader = null, HeaderText = "col1" };
결과가 1
및 new dtHeader(){ ParentHeader = new dtHeader(){ ParentHeader = null, HeaderText = "col1" }, HeaderText = "col1" };
에 대한 결과가 나는 list<dtHeader>
와이를 달성하려면 2
될 것 같은 개체가 말할 수 있습니다, 그래서 그들 중 일부는있을 것입니다 깊이가 1 인 것, 깊이가 깊은 것, 깊이가 가장 깊은 것.
_______ITEM_IN_LIST_OBJECT__
______1___2___3___4___5___6_
D 1. |_o_|_o_|_o_|_o_|_o_|_o_|
E 2. |_o_|___|_o_|___|_o_|_o_|
P 3. |___|___|_o_|___|_o_|___|
T 4. |___|___|___|___|_o_|___|
H 5. |___|___|___|___|_o_|___|
무한히 (객체가 서로 내부로 쌓일 수있을 때까지) 반드시 가야합니다.
var HeaderLayerCount = lDtCol.Where(n => n.ParentHeader != null)
.Where(n => n.ParentHeader.ParentHeader != null)
.Where(n => n.ParentHeader.ParentHeader.ParentHeader != null);
편집 : 난 그냥 당신이 특정 깊이 수준에서 작업 할 경우, 예를 들어, 3의 깊이에있는 모든 개체를, 당신은 클래스에이 여분의 재귀 기능을 사용할 수있는 추가 할
public class dtCol
{
public dtCol ParentHeader { get; set; }
public string HeaderText { get; set; }
public string DataField { get; set; }
public bool Visible { get; set; }
public int DisplayOrder { get; set; }
public int Depth { get { return ParentHeader != null ? ParentHeader.Depth + 1 : 1; } }
public int CurrentDepth { get; set; } //Set on initialisation
public dtCol getParent(dtCol col, int getDepth) //Gets the parent on a specific level after the first base level (1) else returns the previous not null child
{
return (col.ParentHeader != null && col.ParentHeader.CurrentDepth == getDepth) ? col.ParentHeader : this.getParent(col.ParentHeader, getDepth);
}
}
당신은 너무처럼 사용할 수 있습니다
var HeaderLayerCount = lDtCol.OrderByDescending(n => n.Depth).First().Depth;
for (int hlc = 1; hlc <= HeaderLayerCount; hlc++)
{
var headerrow = new List<dtCol>();
//This foreach adds the parent header if not null else adds the not null child
lDtCol.ForEach(n =>
{
var h = n.getParent(n, hlc); //Get Parent, null is returned if parent does not exists
headerrow.Add((h != null) ? h : n); //If parent is null, add base dtCol so that the headers can be merged upwards.
});
//Do what you need with your new single dimensional list of objects
}
답변 해 주셔서 감사합니다. – Pierre