将list转成tree
using System;
using System.Collections.Generic;
using System.Linq;
namespace Infrastructure
{
/// <summary>
/// List转成Tree
/// <para></para>
/// </summary>
public static class GenericHelpers
{
/// <summary>
/// Generates tree of items from item list
/// </summary>
///
/// <typeparam name="T">Type of item in collection</typeparam>
/// <typeparam name="K">Type of parent_id</typeparam>
///
/// <param name="collection">Collection of items</param>
/// <param name="idSelector">Function extracting item's id</param>
/// <param name="parentIdSelector">Function extracting item's parent_id</param>
/// <param name="rootId">Root element id</param>
///
/// <returns>Tree of items</returns>
public static IEnumerable<TreeItem<T>> GenerateTree<T, K>(
this IEnumerable<T> collection,
Func<T, K> idSelector,
Func<T, K> parentIdSelector,
K rootId = default(K))
{
foreach (var c in collection.Where(u =>
{
var selector = parentIdSelector(u);
return (rootId == null && selector == null)
|| (rootId != null &&rootId.Equals(selector));
}))
{
yield return new TreeItem<T>
{
Item = c,
Children = collection.GenerateTree(idSelector, parentIdSelector, idSelector(c))
};
}
}
/// <summary>
/// 把数组转为逗号连接的字符串
/// </summary>
/// <param name="data"></param>
/// <param name="Str"></param>
/// <returns></returns>
public static string ArrayToString(dynamic data, string Str)
{
string resStr = Str;
foreach (var item in data)
{
if (resStr != "")
{
resStr += ",";
}
if (item is string)
{
resStr += item;
}
else
{
resStr += item.Value;
}
}
return resStr;
}
}
}
using System.Collections.Generic;
namespace Infrastructure
{
public class TreeItem<T>
{
public T Item { get; set; }
public IEnumerable<TreeItem<T>> Children { get; set; }
}
}
将list转成tree的更多相关文章
- PHP 把返回的数据集转换成Tree树
/** * 把返回的数据集转换成Tree * @access public * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * ...
- js 一维数组转成tree 对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- easyUi中的一段漂亮代码之将list转换成tree.
function convert(rows){ function exists(rows, parentId){ for(var i=0; i<rows.length; i++){ if (ro ...
- 【技术宅6】把一个无限级分类循环成tree结构
function list_to_tree($list,$root=0,$pk='cid',$pid = 'pid',$child = '_child'){ if(is_array($list)) { ...
- java的List列表转成Tree(树形)结构列表
直接看借鉴博客:https://blog.csdn.net/massivestars/article/details/53911620/ 由于我的业务没有父子级id两个字段,只有一个层级id字段来分层 ...
- list 转成 tree
package com.zl; import java.util.ArrayList; import java.util.List; public class MenuItem { private S ...
- JS将扁平化的数据处理成Tree结构
let jsonData= [ { id:1, parentId:0, name:"一级菜单A" }, { id:2, parentId:0, name:"一级菜单B& ...
- 基于List数组转换成tree对象
package com.shjysoft.yunxi.sync.webservice; import java.util.ArrayList;import java.util.Date;import ...
- jquery easyui tree动态加载子节点
1.前端tree绑定时,使用onBeforeExpand事件:当节点展开时触发加载子节点,自动会向服务端发送请求:url为绑定url,参数为当前节点id this.tree = { method: ' ...
随机推荐
- 统计web 访问日志的请求数据
tomcat日志格式 在配置文件 server.xml 中,具体参照官方文档 https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#A ...
- python format输出
http://www.cnblogs.com/nulige/p/6115793.html 2.Format 方式 [[fill]align][sign][#][0][width][,][.precis ...
- JDK的安装与环境变量配置
1.下载JDK后安装,此处安装的是JDK8 2.安装后的路径如下图所示,JDK与JRE在同一个文件夹中 3.安装完JDK后配置环境变量 计算机→属性→高级系统设置→高级→环境变量 4.系统变量→新建 ...
- latex学习笔记----基本知识、文档排版
1.空格和制表符等空白字符视为相同的空白距离,多个连续的空白字符等同为一个字符. 2.# $ % ^ _ { } ~ 在这些字符前面加上反斜线,就可以在文本中得到它们. 反斜线\不 ...
- Python—使用Json序列化Datetime类型
import json from datetime import datetime, date """ str,int,list,tuple,dict,bool,None ...
- Python 学习笔记:Python 操作 SQL Server 数据库
最近要将数据写到数据库里,学习了一下如何用 Python 来操作 SQL Server 数据库. 一.连接数据库: 首先,我们要连接 SQL Server 数据库,需要安装 pymssql 这个第三方 ...
- 给select赋值之后,再点击选择下拉值时,显示一值不变的解决
在一个项目需求中,请求数据,得到的数据dataAll渲染到页面的select下拉表单中,当时是需要一进页面就要默认选中第一个选项,所以直接将dataAll的第一个索引值赋值给了表单的绑定值formVa ...
- 让几个横向排列的浮动子div居中显示的方法
div设置成float之后,就无法使子div居中显示了,那么如何让几个横向排列的浮动的div居中显示呢,下面有个不错的方法,希望对大家有所帮助 div设置成float之后,在父div中设置text-a ...
- 有关iOS热更新
iOS热更新的几篇文章,看完这几篇,自己集成一下.下面说一下我集成时遇到的问题. 这是原作者的JSPatch的讲解的文章:<JSPatch – 动态更新iOS APP>.<JSPat ...
- python 并发执行
并发执行, 精简代码. 适用python2 和python3 # -*- encoding:utf-8 -*- from threading import Thread from multiproce ...