用linq过滤树

时间:2016-10-24 18:35:44

标签: c# linq filter tree

我有这个结构:

public class issue
{
    public int ID {get; set;}
    public int? ParentID {get; set;}
    public string Type {get;set;}
    public List<issue> Children {get;set;}
}

我的树是:

parent1 ={ID = 1,
           ParentID = null,
           Type = "Category",
           Children = {children1 = {ID = 2,
                       ParentID = 1,
                       Type = "Project",
                        Children = {children2 = {ID = 3,
                                                ParentID = 2,
                                                Type = "Action",
                                                Children = null}}},
                                    {children3 = {ID = 4,
                                                  ParentID = 1,
                                                  Type = "Action",
                                                  Children = null}}}
parent2 = {ID = 5,
           ParentID = null,
           Type = "Category",
           Children = {children4 = {ID = 6,
                                    ParentID = 5,
                                    Type = "Action",
                                    Children = null}}}
parent3 = {ID = 7,
            ParentID = null,
            Type = "Project",
            Children = {children5 = {ID = 8,
                                    ParentID = 7,
                                    Type = "Action",
                                    Children = null}}}

我需要以这种方式过滤这个树,在第一级只出现满足过滤器的父项加上父项树中不包含的子项,例如,我想过滤类型:&#34;类别&#34;和&#34;动作&#34;,树的结果大部分是:

parent1 ={ID = 1,
           ParentID = null,
           Type = "Category",
           Children = {children1 = {ID = 2,
                       ParentID = 1,
                       Type = "Project",
                        Children = {children2 = {ID = 3,
                                                ParentID = 2,
                                                Type = "Action",
                                                Children = null}}},
                                    {children3 = {ID = 4,
                                                  ParentID = 1,
                                                  Type = "Action",
                                                  Children = null}}}
parent2 = {ID = 5,
           ParentID = null,
           Type = "Category",
           Children = {children4 = {ID = 6,
                                    ParentID = 5,
                                    Type = "Action",
                                    Children = null}}}
parent3 = {ID = 8,
            ParentID = 7,
            Type = "Action",
            Children = null}}

使用LINQ有什么办法吗?

0 个答案:

没有答案
相关问题