【Linq递归查找系列】

时间:2021-07-11 05:13:52

Linq递归查找:

 public IEnumerable<MenuInfo> GetTree(int id, IEnumerable<MenuInfo> lst)
        {

            var query = from c in lst
                        where c.parent_menu_id == id
                        select c;
            return query.Concat(query.SelectMany(t => GetTree(t.menu_id, lst))).ToList();

        }

调用如下:

  int groupId = WebUtil.RequestInt("groupId");
            int userId = CurrentUserId;
            int moduleId = this.Module;
            List<MenuInfo> lstMenu = bllMenu.Cache_GetAllMenuInfoByUserId(userId);
            var temList = lstMenu.Where(p => p.menu_type == moduleId);
            var lst = GetTree(groupId, temList);//.GetTree(p => p.parent_menu_id == groupId);