Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 层次结构列表<;定制>;,返回轨道_C#_Asp.net Mvc_Algorithm_Design Patterns_Entity Framework 4 - Fatal编程技术网

C# 层次结构列表<;定制>;,返回轨道

C# 层次结构列表<;定制>;,返回轨道,c#,asp.net-mvc,algorithm,design-patterns,entity-framework-4,C#,Asp.net Mvc,Algorithm,Design Patterns,Entity Framework 4,这可能只是一些逻辑问题。。还是我的设计问题 当前,跟踪功能以跟踪层次结构存在障碍 我有一个自定义的团队课程 public class Team { public int PositionID; public int? LeaderId; public List<Team> Members = new List<Team>(); public Team(int positionid, int? le

这可能只是一些逻辑问题。。还是我的设计问题 当前,跟踪功能以跟踪层次结构存在障碍

我有一个自定义的团队课程

  public class Team
    {
        public int PositionID;
        public int? LeaderId;
        public List<Team> Members = new List<Team>();
        public Team(int positionid, int? leaderid)
        {
            PositionID = positionid;
            LeaderId= leaderid;
        }
    } 
即使在找到节点之后

我想跟踪导航

这个功能的另一个词是让我返回直接领导

我想让直接领导>领导>领导>直到主要领导(领导没有领导)


我不介意返回一个列表,其中列表最小索引0是主前导,最大索引是直接前导

我建议为IEnumarable编写扩展类

public static Team GetChildNode(List<Team> nodes, int seekId)
{
    Team found = null;
    //store leaders of leader that lead to the seek leader.
    List<Team> Leaders= new List<Team>();

    foreach (Team node in nodes)
    {
        if (found == null)
        {
            if (node.PositionID == seekId)
            {
                found = node;
                return found;
            }
            else
            {
                if (node.Members.Count > 0)
                {
                    return GetChildNode(node.Members, seekId);
                }
            }
        } 
    }
    return found;
 }
static class EnumerableExtension
{
    public static IEnumerable<IEnumerable<TSt>> FindNodes<T,TSt>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childrenSelector, Func<T,TSt> resultSelector, Func<T, bool> predicate)
    {
        var result = new List<List<TSt>>();

        Action<T, List<TSt>> search = null;
        search = (element, resultStruct) =>
            {
                resultStruct.Add(resultSelector(element));
                if (predicate(element))
                {
                    result.Add(resultStruct);
                }
                else
                {
                    foreach (var item in childrenSelector(element))
                    {                            
                        search(item,new List<TSt>(resultStruct));
                    }
                }
            };
        foreach (var v in source)
        {
            search(v, new List<TSt>());
        }

        return result;
    }
}

很好的建议,使用这种方法,我似乎得到了层次结构列表,我正在努力注入一种逻辑,跟踪从成员AB到成员CC的路径。最好返回我AB,AA,CA,CB,CC。。。是这样的吗?
static class EnumerableExtension
{
    public static IEnumerable<IEnumerable<TSt>> FindNodes<T,TSt>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childrenSelector, Func<T,TSt> resultSelector, Func<T, bool> predicate)
    {
        var result = new List<List<TSt>>();

        Action<T, List<TSt>> search = null;
        search = (element, resultStruct) =>
            {
                resultStruct.Add(resultSelector(element));
                if (predicate(element))
                {
                    result.Add(resultStruct);
                }
                else
                {
                    foreach (var item in childrenSelector(element))
                    {                            
                        search(item,new List<TSt>(resultStruct));
                    }
                }
            };
        foreach (var v in source)
        {
            search(v, new List<TSt>());
        }

        return result;
    }
}
teams.FindNodes(children => children.Members, res => res.LeaderId, con => con.LeaderId == 100);