using System;
using System.Collections.Generic;
using System.Linq;
namespace Masuit.Tools.Core.Models
{
///
/// 树形数据扩展
///
public static class TreeExtensions
{
///
/// 过滤
///
///
///
///
///
public static IEnumerable Filter(this IEnumerable items, Func func) where T : class, ITree
{
var results = new List();
foreach (var item in items.Where(i => i != null))
{
item.Children = item.Children.Filter(func).ToList();
if (item.Children.Any() || func(item))
{
results.Add(item);
}
}
return results;
}
///
/// 过滤
///
///
///
///
///
public static IEnumerable Filter(this T item, Func func) where T : class, ITree
{
return (new[] { item }).Filter(func);
}
///
/// 平铺开
///
///
///
///
public static IEnumerable Flatten(this IEnumerable items) where T : ITree
{
var result = new List();
foreach (var item in items)
{
result.Add(item);
result.AddRange(item.Children.Flatten());
}
return result;
}
}
}