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的更多相关文章

  1. PHP 把返回的数据集转换成Tree树

    /** * 把返回的数据集转换成Tree * @access public * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * ...

  2. js 一维数组转成tree 对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. easyUi中的一段漂亮代码之将list转换成tree.

    function convert(rows){ function exists(rows, parentId){ for(var i=0; i<rows.length; i++){ if (ro ...

  4. 【技术宅6】把一个无限级分类循环成tree结构

    function list_to_tree($list,$root=0,$pk='cid',$pid = 'pid',$child = '_child'){ if(is_array($list)) { ...

  5. java的List列表转成Tree(树形)结构列表

    直接看借鉴博客:https://blog.csdn.net/massivestars/article/details/53911620/ 由于我的业务没有父子级id两个字段,只有一个层级id字段来分层 ...

  6. list 转成 tree

    package com.zl; import java.util.ArrayList; import java.util.List; public class MenuItem { private S ...

  7. JS将扁平化的数据处理成Tree结构

    let jsonData= [ { id:1,  parentId:0, name:"一级菜单A" }, { id:2, parentId:0, name:"一级菜单B& ...

  8. 基于List数组转换成tree对象

    package com.shjysoft.yunxi.sync.webservice; import java.util.ArrayList;import java.util.Date;import ...

  9. jquery easyui tree动态加载子节点

    1.前端tree绑定时,使用onBeforeExpand事件:当节点展开时触发加载子节点,自动会向服务端发送请求:url为绑定url,参数为当前节点id this.tree = { method: ' ...

随机推荐

  1. bootstrap 基础表单

    表单中常见的元素主要包括:文本输入框.下拉选择框.单选按钮.复选按钮.文本域和按钮等.其中每个控件所起的作用都各不相同,而且不同的浏览器对表单控件渲染的风格都各有不同. ☑   LESS版本:对应源文 ...

  2. 对PHP-GC(垃圾回收)的一点理解

    一直对php的垃圾回收机制不明不白故自己开贴记录下. 首先,说到垃圾回收,得先知道什么情况下才能产生垃圾. 如果一个变量refcount在增加,说明在被使用,不是垃圾. 如果一个变量的refcount ...

  3. h5-渐变的基本描述

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 系统学习python第四天学习笔记

    1.解释 / 编译补充 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. 解释型:写完代码交给解释器,解释器会从上到下一行行代码执行:边解释边执行. [实时翻译] 2.字符串功能 ...

  5. C++对象数组与对象指针

    (一)对象数组 将具有相同类类型的对象有序地集合在一起便构成了对象数组,以一维对象数组为例,其定义形式为: 类名 对象数组名[]; Point points[100]; 关于对象数组的几点说明: (1 ...

  6. Java 中的接口有什么作用?以及接口和其实现类的关系?

    Java 中的接口有什么作用? - Ivony的回答 - 知乎 https://www.zhihu.com/question/20111251/answer/16585393 这是一个初学者非常常见的 ...

  7. 吴裕雄--天生自然TensorFlow2教程:numpy [ ] 索引

    import tensorflow as tf a = tf.ones([1, 5, 5, 3]) a.shape a[0][0] numpy : 索引 a = tf.random.normal([4 ...

  8. 吴裕雄--天生自然 PHP开发学习:数据库 ODBC

    <html> <body> <?php $conn=odbc_connect('northwind','',''); if (!$conn) { exit("连 ...

  9. 03 Mybatis:05.使用Mybatis完成CRUD

    mybatis框架:共四天 明确:我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式.不管使用XML还是注解配置. 第二天:mybatis基本使用 mybatis的单表crud操作 ...

  10. IT架构的本质--阅读笔记01

    万物都有其本质,也只有了解了事物的本质之后,才不至于出现在事物稍作改变时就难以应对的情况,作为软件工程专业的学生,我们应该对IT架构的本质有一定的了解.“老僧三十年前未参禅时,见山是山,见水是水.及至 ...