2020-03-15 23:54:48 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-03-15 23:54:48 +08:00
|
|
|
|
{
|
|
|
|
|
public static class EnumerableExtensions
|
|
|
|
|
{
|
|
|
|
|
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
|
|
|
|
|
{
|
|
|
|
|
if (items == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (T obj in items)
|
|
|
|
|
{
|
|
|
|
|
action(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task ForEachAsync<T>(this IEnumerable<T> items, Func<T, Task> func)
|
|
|
|
|
{
|
|
|
|
|
if (items == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-05 20:27:52 +08:00
|
|
|
|
foreach (var item in items) await func(item);
|
2020-03-15 23:54:48 +08:00
|
|
|
|
}
|
2020-03-22 21:24:41 +08:00
|
|
|
|
|
|
|
|
|
public static bool IsIn<T>(this T source, params T[] array)
|
|
|
|
|
{
|
2020-07-30 23:54:31 +08:00
|
|
|
|
if (array == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 21:24:41 +08:00
|
|
|
|
return array.Contains(source);
|
|
|
|
|
}
|
2020-07-10 17:57:20 +08:00
|
|
|
|
|
|
|
|
|
public static bool IsIn<T>(this T source, IEnumerable<T> array)
|
|
|
|
|
{
|
2020-07-30 23:54:31 +08:00
|
|
|
|
if (array == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
return array.Contains(source);
|
|
|
|
|
}
|
2020-07-30 23:54:31 +08:00
|
|
|
|
|
|
|
|
|
public static T[] Append<T>(this T[] array, T item)
|
|
|
|
|
{
|
|
|
|
|
if (array == null)
|
|
|
|
|
{
|
|
|
|
|
return new[] { item };
|
|
|
|
|
}
|
|
|
|
|
Array.Resize(ref array, array.Length + 1);
|
|
|
|
|
array[^1] = item;
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
2020-08-18 18:05:44 +08:00
|
|
|
|
|
|
|
|
|
public static T[] Remove<T>(this T[] array, T item)
|
|
|
|
|
{
|
|
|
|
|
if (array == null)
|
|
|
|
|
{
|
2020-10-25 19:11:45 +08:00
|
|
|
|
return Array.Empty<T>();
|
2020-08-18 18:05:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 19:11:45 +08:00
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
return array.Where(x => x != null).ToArray();
|
|
|
|
|
}
|
2020-08-18 18:05:44 +08:00
|
|
|
|
|
2020-10-25 19:11:45 +08:00
|
|
|
|
return array.Where(x => !item.Equals(x)).ToArray();
|
2020-08-18 18:05:44 +08:00
|
|
|
|
}
|
2020-12-26 21:39:04 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// add item to items when condition is true
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="items"></param>
|
|
|
|
|
/// <param name="condition"></param>
|
|
|
|
|
/// <param name="item"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IList<T> AddIf<T>(this IList<T> items, bool condition, T item)
|
|
|
|
|
{
|
|
|
|
|
items ??= new List<T>();
|
|
|
|
|
if (condition)
|
|
|
|
|
{
|
|
|
|
|
items.Add(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
2020-03-15 23:54:48 +08:00
|
|
|
|
}
|
2020-06-05 16:06:23 +08:00
|
|
|
|
}
|