|
/// <summary>
/// 枚举接口扩展方法
/// </summary>
public static class EnumerableExtensions
{
/// <summary>
/// 对枚举源的每一项调用指定的方法
/// </summary>
/// <typeparam name="T">枚举源数据类型</typeparam>
/// <param name="items">枚举源</param>
/// <param name="action">每一项需要执行的方法</param>
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
Requires.NotNull(items, "items");
Requires.NotNull(action, "action");
foreach (T current in items)
action(current);
return items;
}
/// <summary>
/// 获取最后一个子元素
/// 超出位置或空的集合则返回null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <returns></returns>
public static T LastChild<T>(this IEnumerable<T> items)
{
if (items == null)
return default(T);
int count = items.Count();
if (count <= 0)
return default(T);
return items.ElementAt(count - 1);
}
/// <summary>
/// 获取第一个元素
/// 超出位置或空的集合则返回null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <returns></returns>
public static T FirstChild<T>(this IEnumerable<T> items)
{
if (items == null)
return default(T);
int count = items.Count();
if (count <= 0)
return default(T);
return items.ElementAt(0);
}
/// <summary>
/// 获取指定位置的元素
/// 超出位置或空的集合则返回null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <param name="index"></param>
/// <returns></returns>
public static T GetPositionChild<T>(this IEnumerable<T> items, int index)
{
if (items == null)
return default(T);
if (index < 0)
return default(T);
int count = items.Count();
if (count <= 0)
return default(T);
if (index >= count)
return default(T);
return items.ElementAt(index);
}
/// <summary>
/// 将一个集合中的数据加入到列表中
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="target">要操作的列表</param>
/// <param name="collection">要添加的数据集合</param>
public static IList<T> AddRange<T>(this IList<T> target, IEnumerable<T> collection)
{
Requires.NotNull(target, "target");
Requires.NotNull(collection, "collection");
foreach (T current in collection)
target.Add(current);
return target;
}
/// <summary>
/// 返回集合都存在的集合(交集)
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TFilter"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static IEnumerable<TSource> Intersect<TSource, TFilter>(this IEnumerable<TSource> first, IEnumerable<TFilter> second, Func<TSource, TFilter, bool> predicate)
{
var filters = new List<TSource>();
foreach (var t in first)
foreach (var s in second)
if (predicate(t, s)) filters.Add(t);
return filters;
}
/// <summary>
/// 比较过滤集合,返回过滤集合中不存在的集合(差集)
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TFilter"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static IEnumerable<TSource> Except<TSource, TFilter>(this IEnumerable<TSource> first, IEnumerable<TFilter> second, Func<TSource, TFilter, bool> predicate)
{
var filters = new List<TSource>();
foreach (var t in first)
{
if (!second.Any(x => predicate(t, x)))
filters.Add(t);
}
return filters;
}
}
|
|