public dynamic GetDepartments(string labID)
{
List<int> usedIDs = new List<int>(); //缓存已用过的ID //定义递归算法
Func<object,List<DepartmentItem>, List<DepartmentItem>, dynamic> recursion = (r,d,a) =>
{
List<dynamic> dyData = new List<dynamic>();
if (d.Count > 0)
{
d.ForEach(x =>
{
if (!usedIDs.Contains(x.DepartmentID))
{
dynamic department = new ExpandoObject();
department.DepartmentID = x.DepartmentID;
department.DepartmentName = x.DepartmentName;
department.DepartmentDesc = x.DepartmentDesc;
department.CreateTime = x.CreateTime;
department.ParentDepartmentID = x.ParentDepartmentID;
usedIDs.Add(x.DepartmentID);
// department.children = recursion(r, d, a); 这里直接调用的话会提示未定义变量,因为func的定义还没结束..
// 所以用以下的方法:调用的时候用object把方法传进来,然后转换下再调用执行;
var func_recursion = r as Func<object, List<DepartmentItem>, List<DepartmentItem>, dynamic>;
department.children = func_recursion(r, a.Where(w => w.ParentDepartmentID == x.DepartmentID).ToList(), a);
dyData.Add(department);
}
});
} return dyData;
};
var departments =.....//获取集合数据; //调用方法
//这里第一个参数就是方法的本身,传递进去进行递归调用
var result = recursion(recursion, departments, departments); return result; }

  代码是将集合对象转换成树形结构

记一则 Lambda内递归调用方法将集合对象转换成树形结构的更多相关文章

  1. OC 继承子类对象调用方法机制 子类对象访问父类中的实例变量

    在继承中,子类对象如何调用到正确方法的机制 每一个Objective - C对象都有一个隐藏的指针指向类的代码,当向一个对象发送消息的时候,当前的对象会首先在当前类里去查找相应的方法,如果找到的话,直 ...

  2. C#调用WPS将文档转换成pdf进行预览

    引用:https://www.jianshu.com/p/445996126c75 vs启动项目可以生成wps实例 本地iis部署的站点却不行 原因是vs是管理员权限,而iis没有权限 解决方法 启动 ...

  3. js方法实现rgb颜色转换成16进制格式的代码的方法

    原文地址:http://www.cnblogs.com/vaal-water/archive/2013/04/08/3008880.html 自己试过很好用 function zero_fill_he ...

  4. 把dataset对象转换成list集合方法

    public static List<T> GetList<T>(DataTable table) where T:new() { List<T> list = n ...

  5. parseObject方法将json字符串转换成Map

    String nwVal=recordDO.getWorkOrderNwVal(); HashMap<String,WxhcWorkOrderDO> nwMap=JSON.parseObj ...

  6. PHP将对象转换成数组的方法(兼容多维数组类型)

    /** * @author gayayang * @date 2012-8-21 * @todo 将对象转换成数组 * @param unknown_type $obj * @return unkno ...

  7. JS - 把类似document.querySelectorAll(".xxx")、document.getElementsByName("xxx")这种方法的返回结果转换成数组对象

    var btns = document.querySelectorAll(".btn");console.log(btns instanceof Array); // falseb ...

  8. 使用fastjson的parseObject方法将json字符串转换成Map 或者List

    fastjson 转换成map HashMap<String,String> map = JSON.parseObject(jsonStr,new TypeReference<Has ...

  9. Array.prototype.slice.call()等几种将arguments对象转换成数组对象的方法

    网站搬迁,给你带来的不便敬请谅解! http://www.suanliutudousi.com/2017/10/10/array-prototype-slice-call%E7%AD%89%E5%87 ...

随机推荐

  1. Utterance-level Aggregation for Speaker Recognition in The Wild

    文章[1]主要针对的是语句长度不定,含有不相关信号的说话人识别. 深度网络设计的关键在于主干(帧级)网络的类型[the type of trunk (frame level) network]和有时间 ...

  2. LeetCode 622. Design Circular Queue

    原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the c ...

  3. 20165214 2018-2019-2 《网络对抗技术》Exp7 网络欺诈防范 Week10

    <网络对抗技术>Exp7 网络欺诈防范 Week10 一.实验目标与内容 1.实践目标 理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法 2.实践内容 (1)简单应用SET工 ...

  4. SQL Server表 & 存储过程 创建日期查询

    查询表创建时间 SELECT [name] ,create_date ,modify_date FROM sys.tables ORDER BY modify_date DESC 查下存储过程创建时间 ...

  5. Golang微服务实践

    背景 在之前的文章<漫谈微服务>我已经简单的介绍过微服务,微服务特性是轻量级跨平台和跨语言的服务,也列举了比较了集中微服务通信的手段的利弊,本文将通过RPC通信的方式实现一个增删查Redi ...

  6. 搭建 Docker Swarm 集群

      准备三台主机 A:192.168.1.5 B:192.168.1.7 C:192.168.1.10   Docker Swarm集群中的节点主机开放以下三个端口 2377端口, 用于集群管理通信 ...

  7. Akka-CQRS(7)- CQRS Reader Actor 示范

    我们在这篇通过一个具体CQRS-Reader-Actor的例子来示范akka-persistence的query端编程和应用.在前面的博客里我们设计了一个CQRS模式POS机程序的操作动作录入过程,并 ...

  8. 【Pandas教程】像写SQL一样用Pandas~

    写在最前 Python在数据分析领域有三个必须需要熟悉的库,分别是pandas,numpy和matplotlib,如果排个优先级的话,我推荐先学pandas. numpy主要用于数组和矩阵的运算,一般 ...

  9. 【题解】整数划分 [51nod1201] 整数划分 V2 [51nod1259]

    [题解]整数划分 [51nod1201] 整数划分 V2 [51nod1259] 传送门:整数划分 \([51nod1201]\) 整数划分 \(V2\) \([51nod1259]\)** [题目描 ...

  10. RocketMQ Release Note(RocketMQ升级日志译文)

    RocketMQ升级日志 1 4.2.0 原版Release Note 1.1 New Feature 支持传输层安全性 客户端支持log4j2 PushConsumer支持条数与大小维度的流控 1. ...