c# - 使用 LAMBDA 表达式获取具有相同类型的对象的对象树的深度

标签 c# algorithm object lambda generic-list

我有这个对象:

public class dtHeader
{
    public dtHeader ParentHeader { get; set; }
    public string HeaderText { get; set; }
    public string DataField { get; set; }
    public bool Visible { get; set; }
    public int DisplayOrder { get; set; }
}

我想用lambda表达式计算物体的深度,物体本身存在多少层?

我看到了this JavaScript post ,但我正在努力将其转换为单行 lambda 语句。

假设对象是这样的 new dtHeader(){ ParentHeader = null, HeaderText = "col1" }; 结果将是 1

new dtHeader(){ ParentHeader = new dtHeader(){ ParentHeader = null, HeaderText = "col1" }, HeaderText = "col1" };结果将是 2

我想用 list<dtHeader> 来实现这一点, 所以他们中的一些人的深度为 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
}

最佳答案

为什么不在你的类上实现一个 int GetDepth() 方法,它会到达最顶层的祖先,计算每个级别?

这样您的查询就会简单得多。

我被 Frode 打败了,感谢他

我有相同的实现:

 public int GetDepth()
        {
            if (ParentHeader == null)
            {
                return 1;
            }
            else return 1 + ParentHeader.GetDepth();
        }

关于c# - 使用 LAMBDA 表达式获取具有相同类型的对象的对象树的深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21308457/

相关文章:

c# : Type. 从 exe 类型字符串上的 dll 调用的 GetType

c++ - 如何找到 vector 中的最大元素 (C++)?

php - 你将如何在没有循环的情况下重写它?

javascript - 在javascript中将一个简单的java对象重新排序为3级对象

Javascript - 试图在动态属性中包含对象

javascript - Angular JS在textarea中显示和编辑模型

c# - 使用委托(delegate)将方法签名传递给线程

c# - 为什么会发生此错误 'Sequence contains no elements' ?

c# - 奇怪的 : WinForms form closes automatically after button press

c++ - 难以理解的简单递归排序算法