mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-15 14:04:41 +08:00
routine update
This commit is contained in:
parent
8fca8cb00e
commit
e68555e976
42
Infrastructure/GenericHelpers.cs
Normal file
42
Infrastructure/GenericHelpers.cs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Infrastructure
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// List转成Tree
|
||||||
|
/// <para>李玉宝新增于2016-10-09 19:54:07</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(c => parentIdSelector(c).Equals(rootId)))
|
||||||
|
{
|
||||||
|
yield return new TreeItem<T>
|
||||||
|
{
|
||||||
|
Item = c,
|
||||||
|
Children = collection.GenerateTree(idSelector, parentIdSelector, idSelector(c))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
Infrastructure/TreeItem.cs
Normal file
10
Infrastructure/TreeItem.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Infrastructure
|
||||||
|
{
|
||||||
|
public class TreeItem<T>
|
||||||
|
{
|
||||||
|
public T Item { get; set; }
|
||||||
|
public IEnumerable<TreeItem<T>> Children { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user