1

这是一个实体,我想在通用函数中列出给定节点的所有子节点

    public static List<T> BuildTree<T>(List<T> list, T selectNode string keyPropName, string parentPropName, string levelPropName, int level = 0)
    {
        List<T> entity = new List<T>();

        foreach (T item in list)
        {


        }


        return entity;
    }

实体结构示例

    protected long _coakey;
    protected long _parentkey;
    protected string _coacode;
    protected string _coacodeclient;
    protected string _coaname;
    protected int _coalevel;

    [DataMember]
    public long coakey
    {
        get { return _coakey; }
        set { _coakey = value; this.OnChnaged(); }
    }

    [DataMember]
    public long parentkey
    {
        get { return _parentkey; }
        set { _parentkey = value; this.OnChnaged(); }
    }

    [DataMember]
    public string coacode
    {
        get { return _coacode; }
        set { _coacode = value; this.OnChnaged(); }
    }

    [DataMember]
    public string coacodeclient
    {
        get { return _coacodeclient; }
        set { _coacodeclient = value; this.OnChnaged(); }
    }

    [DataMember]
    public string coaname
    {
        get { return _coaname; }
        set { _coaname = value; this.OnChnaged(); }
    }

    [DataMember]
    public int coalevel
    {
        get { return _coalevel; }
        set { _coalevel = value; this.OnChnaged(); }
    }
4

3 回答 3

0

你的BuildTree<T>方法不能确定树的结构,除非它知道它的结构。至少,我建议创建一个定义树节点的基类或接口,然后更改BuildTree方法以专门处理这些类型的对象。然后,它将能够找出树结构。你们每个实体类都必须实现该树节点接口或从树节点基类继承。例如:

public abstract class TreeNodeBase
{
    public long parentkey
    {
        get { return _parentkey; }
        set { _parentkey = value; this.OnChanged(); }
    }
    protected long _parentkey;
}

public class MyEntityTreeNode : TreeNodeBase
{
    public long coakey
    {
        get { return _coakey; }
        set { _coakey = value; this.OnChanged(); }
    }
    protected long _coakey;

    // etc...
}

// Note the generic type constraint at the end of the next line
public static List<T> BuildTree<T>(List<T> list, T selectNode, string keyPropName, string parentPropName, string levelPropName, int level) where T : TreeNodeBase
{
    List<T> entity = new List<T>();
    foreach (TreeNodeBase node in list)
    {
        long parentKey = node.parentkey;
        // etc...
    }
    return entity;
}
于 2012-06-19T12:38:10.403 回答
0

我自己解决了希望对你有帮助

    public static List<T> BuildTree<T>(List<T> list, T selectedNode, string keyPropName, string parentPropName, int endLevel = 0, int level = 0)
    {
        List<T> entity = new List<T>();
        Type type = typeof(T);
        PropertyInfo keyProp = type.GetProperty(keyPropName);
        string _selectedNodekey = keyProp.GetValue(selectedNode, null).ToString();

        PropertyInfo parentProp = type.GetProperty(parentPropName);
        foreach (T item in list)
        {

            string _key = keyProp.GetValue(item, null).ToString();
            string _parent = parentProp.GetValue(item, null).ToString();

            if (_selectedNodekey == _parent)
            {
                T obj = (T)Activator.CreateInstance(typeof(T));
                obj = item;
                entity.Add(obj);
                if (level == endLevel && level!=0) break;
                entity.AddRange(BuildTree<T>(list, obj, keyPropName, parentPropName, level + 1));
            }
        }


        return entity;

    }
于 2012-06-19T13:03:35.183 回答
0

节点类:

public class Node<TKey, TValue> where TKey : IEquatable<TKey>
{
    public TKey Key { get; set; }
    public TKey ParentKey { get; set; }
    public TValue Data { get; set; }

    public readonly List<Node<TKey, TValue>> Children = new List<Node<TKey, TValue>>();
}

树生成器:

public static Node<TKey, TValue> BuildTree<TKey, TValue>(IEnumerable<Node<TKey, TValue>> list,
                                                         Node<TKey, TValue> selectNode)
    where TKey : IEquatable<TKey>
{
    if (ReferenceEquals(selectNode, null))
    {
        return null;
    }

    var selectNodeKey = selectNode.Key;

    foreach (var childNode in list.Where(x => x.ParentKey.Equals(selectNodeKey)))
    {
        selectNode.Children.Add(BuildTree(list, childNode));
    }

    return selectNode;
}

用法:

List<MyClass> list = ...

var nodes = list.Select(x => new Node<long, MyClass>
                                 {
                                     Key = x.MyKey,
                                     ParentKey = x.MyParentKey,
                                     Data = x
                                 }).ToList();

var startNode = nodes.FirstOrDefault(x => x.Data.Stuff == "Pick me!");

var tree = BuildTree(nodes, startNode);

MyClass 示例:

public class MyClass
{
    public long MyKey;
    public long MyParentKey;
    public string Name;
    public string Text;
    public string Stuff;
}
于 2012-06-19T13:07:13.553 回答