123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- namespace Ips.Library.Basic
- {
- public static class EnumerableExtensions
- {
- public static bool IsNullOrEmpty<T>(this IEnumerable<T> @this)
- {
- return @this == null || !@this.Any();
- }
- public static bool IsNotNullOrEmpty<T>(this IEnumerable<T> @this)
- {
- return @this != null && @this.Any();
- }
- public static string JoinAsString(this IEnumerable<string> source, string separator)
- {
- return source.Any() ? string.Join(separator, source) : "";
- }
- public static string JoinAsString<T>(this IEnumerable<T> source, string separator)
- {
- return source.Any() ? string.Join(separator, source) : "";
- }
- public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, bool condition, Func<T, bool> predicate)
- {
- return condition
- ? source.Where(predicate)
- : source;
- }
- public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, bool condition, Func<T, int, bool> predicate)
- {
- return condition
- ? source.Where(predicate)
- : source;
- }
- public static IEnumerable<T> ForEach<T>(this IEnumerable<T> array, Action<T> act)
- {
- foreach (var i in array)
- act(i);
- return array;
- }
- public static IEnumerable<T> ForEach<T>(this IEnumerable arr, Action<T> act)
- {
- return arr.Cast<T>().ForEach<T>(act);
- }
- public static IEnumerable<RT> ForEach<T, RT>(this IEnumerable<T> array, Func<T, RT> func)
- {
- var list = new List<RT>();
- foreach (var i in array)
- {
- var obj = func(i);
- if (obj != null)
- list.Add(obj);
- }
- return list;
- }
- public static Dictionary<TKey, List<TValue>> ToDictionary<TKey, TValue>(this IEnumerable<IGrouping<TKey, TValue>> groupings)
- {
- return groupings.ToDictionary(group => group.Key, group => group.ToList());
- }
- }
- }
|