using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace LifePayment.Domain.Shared;
///
/// 集合扩展
///
public static class ListExtensions
{
///
/// 异步循环
///
///
///
///
///
public static async Task ForEachAsync(this List list, Func action)
{
foreach (var value in list)
{
await action(value);
}
}
///
/// 异步循环
///
///
///
///
///
public static async Task ForEachAsync(this IEnumerable source, Func action)
{
foreach (var value in source)
{
await action(value);
}
}
///
/// 是否为null或空集合
///
///
///
public static bool IsNullOrEmpty(this IEnumerable source)
{
return (source == null || !source.HasItems());
}
///
/// 是否不为null或空集合
///
///
///
public static bool IsNotNullOrEmpty(this IEnumerable source)
{
return !IsNullOrEmpty(source);
}
///
/// 是否有元素
///
///
///
public static bool HasItems(this IEnumerable source)
{
return source != null && source.GetEnumerator().MoveNext();
}
/////
///// 根据指定间隔字符将集合合并为字符串
/////
/////
/////
/////
//public static string JoinAsString(this IEnumerable source, string separator)
//{
// return string.Join(separator, source);
//}
///
/// 根据指定间隔字符将集合合并为字符串
///
///
///
///
///
public static string JoinAsString(this IEnumerable source, string separator)
{
return string.Join(separator, source);
}
///
/// ICollection批量添加元素
///
///
///
///
///
public static void AddRange(this ICollection collection, IEnumerable items)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (items == null)
throw new ArgumentNullException(nameof(items));
if (collection is List list)
{
list.AddRange(items);
}
else
{
foreach (var item in items)
{
collection.Add(item);
}
}
}
///
/// 转换为值为object类型的字典
///
///
///
///
///
public static Dictionary ToObjectDic(this IDictionary resource) where TV : class
{
var result = new Dictionary();
foreach (var item in resource)
{
result.Add(item.Key, (object)item.Value);
}
return result;
}
}