C#中的嵌套对象过滤

时间:2016-12-14 11:24:15

标签: c# collections

我有菜单对象的集合,每个菜单对象可以有多个子菜单,也可以有多个子菜单等等。

MenuID
MenuName
IsActive
Children
       MenuID
       MenuName
       IsActive
       Children
               MenuID
               MenuName
               IsActive
               Children

我想只过滤活动菜单。怎么做?

我尝试递归但没有运气。

private void FilterDeletedRecord(List<Menu> menus)
        {
            if (menus != null && menus.Count > 0)
            {
                foreach (Menu item in menus)
                {
                    if (item.Children != null && item.Children.Count > 0)
                    {
                        item.Children = item.Children.Where(x => !x.IsDeleted).ToList();
                        if (item.Children != null && item.Children.Count > 0)
            {
                            foreach (Menu m in item.Children)
                            {
                                if (m.Children != null && m.Children.Count > 0)
                                {
                                    FilterDeletedRecord(m.Children);
                                }
                            }
            }
                    }
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

将新属性添加到您的菜单类并使用它:

public class Menu
{
    //Other Memebrs

    public IEnumerable<Menu> ActiveMenus
    {
        get
        {
            return Childeren?.Where(s => !s.IsDeleted);
        }
    }
}

答案 1 :(得分:0)

你可以试试这个;

    IEnumerable<Menu> GetActiveMenus(Menu menu)
    {
        if (menu.IsActive)
        {
            yield return menu;
        }
        if (menu.Children == null)
        {
            yield break;
        }
        foreach (var child in menu.Children)
        {
            foreach (var item in GetActiveMenus(child))
            {
                yield return item;
            }
        }
    }

仅返回IsActive属性为true的菜单。如果你对返回&#34; IsActive&#34;不感兴趣您可以通过调整if(menu.IsActive)行中的逻辑来更改此方法返回的菜单。

如果你想测试它,我创建了这个控制台应用程序;

using System;
using System.Collections.Generic;

namespace ConsoleApplication26
{
    class Program
    {
        static void Main(string[] args)
        {
            var menu = new Menu
            {
                MenuID = 0,
                IsActive = false,
                Children =
                    new List<Menu>
                    {
                        new Menu
                        {
                            MenuID = 2,
                            IsActive = true,
                            Children =
                                new List<Menu>
                                {
                                    new Menu {MenuID = 4, IsActive = true},
                                    new Menu {MenuID = 5, IsActive = false}
                                }
                        },
                        new Menu
                        {
                            MenuID = 3,
                            IsActive = true,
                            Children =
                                new List<Menu>
                                {
                                    new Menu
                                    {
                                        MenuID = 12,
                                        IsActive = false,
                                        Children =
                                            new List<Menu>
                                            {
                                                new Menu {MenuID = 7, IsActive = true},
                                                new Menu {MenuID = 8, IsActive = false}
                                            }
                                    },
                                    new Menu {MenuID = 11, IsActive = true}
                                }
                        }
                    }


            };

            var activeMenus = GetActiveMenus(menu);
            foreach (var activeMenu in activeMenus)
            {
                Console.WriteLine(activeMenu.MenuID);
            }
            Console.ReadLine();
        }

        static IEnumerable<Menu> GetActiveMenus(Menu menu)
        {
            if (menu.IsActive)
            {
                yield return menu;
            }
            if (menu.Children == null)
            {
                yield break;
            }
            foreach (var child in menu.Children)
            {
                foreach (var item in GetActiveMenus(child))
                {
                    yield return item;
                }
            }
        }

    }

    class Menu
    {
        public string MenuName { get; set; }
        public bool IsActive { get; set; }
        public int MenuID { get; set; }
        public IEnumerable<Menu> Children { get; set; }
    }
}