将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: ' ...
随机推荐
- 使用Matplotlib
1.Matplotlib是python的一个绘图库. 2.from matplotlib import pyplot as plt
- Python—二叉树数据结构
二叉树 简介: 二叉树是每个结点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). 二叉树二叉树的链式存储: 将二叉树的节点定义为 ...
- 关于图算法 & 图分析的基础知识概览
网址:https://learning.oreilly.com/library/view/graph-algorithms-/9781492060116/ 你肯定没有读过这本书,因为这本书的发布日期是 ...
- Java final 关键字的用法以及原理(7)
/* final : 最终.作为一个修饰符, 1:可以修饰类,函数,变量. 2:被final修饰的类不可以被继承.为了避免被继承,被子类复写功能. 3:被final修饰的方法不可以被复写. 4:被fi ...
- 《Docekr入门学习篇》——Docker镜像制作
Docker镜像制作 Docker镜像的构建分为两种,一种是手动构建,一种是dockerfile(自动构建) 手动构建 基于centos镜像进行构建制作Nginx镜像 [root@rbtnode1 ~ ...
- C++读取数量不定的数据
#include <iostream> using namespace std; int main(){ ,num=; while(cin >> num){//此表达式从标准输 ...
- UML-领域模型-添加关联和属性
1.何谓关联? 关联(association):一个类的全局变量引用了另一个类,就表示关联了这个类 2.何时使用关联? 长时间(需要记住)留存的需要关联:短时间的不需要.比如: 需要关联:老师教那些课 ...
- keyword排序-Es问题
问题:mapping索引registerordercount字段设置为keyword,但是在进行倒序排的视乎发现,没有按预期排序. keyword类型: "registerordercoun ...
- Chrome使用频率最高的快捷键
标签 ctrl+T 打开新标签 ——— ctrl+W 关闭标签 ctrl+shift+T 打开上衣个被关闭的标签 ctrl+tab 标签向右切换 —— ctrl+shift+tab 标签向左切换 c ...
- 2×c列联表|多组比例简式|卡方检验|χ2检验与连续型资料假设检验
第四章 χ2检验 χ2检验与连续型资料假设检验的区别? 卡方检验的假设检验是什么? 理论值等于实际值 何条件下卡方检验的需要矫正?如何矫正? 卡方检验的自由度如何计算? Df=k-1而不是n-1 卡方 ...