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. 逃生(地图上的dp)

    蒜头君在玩一款逃生的游戏.在一个n×m 的矩形地图上,蒜头位于其中一个点.地图上每个格子有加血的药剂,和掉血的火焰,药剂的药效不同,火焰的大小也不同,每个格子上有一个数字,如果格子上的数字是正数说明是 ...

  2. (转)绝对路径${pageContext.request.contextPath}用法及其与web.xml中Servlet的url-pattern匹配过程

    以系统的一个“添加商品”的功能为例加以说明,系统页面为add.jsp,如图一所示: 图一  添加商品界面 系统的代码目录结构及add.jsp代码如图二所示: 图二   系统的代码目录结构及add.js ...

  3. keras_yolo3阅读

    源码地址 https://github.com/qqwweee/keras-yolo3 春节期间仔细看了看yolov3的kears源码,这个源码毕竟不是作者写的,有点寒酸,可能大道至简也是这么个理.我 ...

  4. Dynamics CRM - 在 Dynamics CRM 开发中创建一个 Entity 对象

    在 Dynamics CRM 的开发中,我们时不时需要创建 Entity 对象,而对于如何创建 Entity 对象,在 C# plugin 和 JS 的写法存在些许差异. 一.C# Plugin 创建 ...

  5. zip4j 2.0压缩 加密压缩

    https://github.com/srikanth-lingala/zip4j ZipParameters zipParameters = new ZipParameters(); zipPara ...

  6. Delphi调用c++写的dll (me)

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  7. php多态模拟

    在PHP中,多态是最常用到的一种特性.所谓多态,是指同一个东西不同形态的展示.在PHP中,我们这样定义多态,一个类被多个子类继承,如果这个类的某个方法在多个子类中表现不同的功能,那么这种行为我们就称其 ...

  8. Android studio2.2 app:transformNative_libsWithStripDebugSymbolForDebug

    开始搜到的问题相关链接: http://blog.csdn.NET/doumingliangdendsc/article/details/52595317 https://www.oschina.ne ...

  9. linux配置词典goldendict

    在mint 18下使用通过, ubuntu 类似. 方法: 通过软件中心安装goldendict,或者 sudo apt install goldendict 通过网页抓取程序, 见附录 下载朗道词典 ...

  10. PAT A1133 Splitting A Linked List (25) [链表]

    题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...