| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CommonTool
- {
- /// <summary>
- /// List转成Tree
- /// </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="TK">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, TK>(
- this IEnumerable<T> collection,
- Func<T, TK> idSelector,
- Func<T, TK> parentIdSelector,
- TK rootId = default(TK))
- {
- var enumerable = collection as T[] ?? collection.ToArray();
- foreach (var c in enumerable.Where(c => parentIdSelector(c).Equals(rootId)))
- {
- yield return new TreeItem<T>
- {
- Item = c,
- Children = enumerable.GenerateTree(idSelector, parentIdSelector, idSelector(c))
- };
- }
- }
- }
- }
|