149 lines
4.3 KiB
C#
Executable File
149 lines
4.3 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared
|
|
{
|
|
public static class IEnumerableExtensions
|
|
{
|
|
public static bool IsNullOrEmpty<T>(this IEnumerable<T> t)
|
|
{
|
|
return t == null || !t.Any();
|
|
}
|
|
|
|
public static bool NotNullOrEmpty<T>(this IEnumerable<T> t)
|
|
{
|
|
return t != null && t.Any();
|
|
}
|
|
public static IEnumerable<TSource> Alternate<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
|
|
{
|
|
using (IEnumerator<TSource> e1 = first.GetEnumerator())
|
|
using (IEnumerator<TSource> e2 = second.GetEnumerator())
|
|
{
|
|
while (e1.MoveNext())
|
|
{
|
|
yield return e1.Current;
|
|
if (e2.MoveNext())
|
|
{
|
|
yield return e2.Current;
|
|
}
|
|
}
|
|
while (e2.MoveNext())
|
|
{
|
|
yield return e2.Current;
|
|
}
|
|
}
|
|
}
|
|
public static IEnumerable<TSource> Add<TSource>(this IEnumerable<TSource> source, TSource element)
|
|
{
|
|
using (IEnumerator<TSource> e1 = source.GetEnumerator())
|
|
{
|
|
while (e1.MoveNext())
|
|
{
|
|
yield return e1.Current;
|
|
}
|
|
}
|
|
|
|
yield return element;
|
|
}
|
|
|
|
public static void AddIfNotNull<TSource>(this List<TSource> source, TSource item)
|
|
{
|
|
if (item != null)
|
|
{
|
|
source.Add(item);
|
|
}
|
|
}
|
|
|
|
public static void AddRangeIfNotNull<TSource>(this List<TSource> source, IEnumerable<TSource> items)
|
|
{
|
|
if (items != null)
|
|
{
|
|
source.AddRange(items.Where(i => i != null));
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<TResult> Convert<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> convert)
|
|
{
|
|
Guard.ArgumentNotNull(convert, nameof(convert));
|
|
using (IEnumerator<TSource> e = source.GetEnumerator())
|
|
{
|
|
while (e.MoveNext())
|
|
{
|
|
yield return convert(e.Current);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static DataTable ToDataTable<T>(this IEnumerable<T> items)
|
|
where T : class, new()
|
|
{
|
|
var table = new DataTable(typeof(T).Name);
|
|
|
|
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|
|
|
foreach (PropertyInfo prop in props)
|
|
{
|
|
Type t = prop.PropertyType.GetCoreType();
|
|
table.Columns.Add(prop.Name, t);
|
|
}
|
|
|
|
foreach (T item in items)
|
|
{
|
|
var values = new object[props.Length];
|
|
|
|
for (int i = 0; i < props.Length; i++)
|
|
{
|
|
values[i] = props[i].GetValue(item, null);
|
|
}
|
|
|
|
table.Rows.Add(values);
|
|
}
|
|
|
|
return table;
|
|
}
|
|
|
|
public static string ToQueryString(this System.Collections.Specialized.NameValueCollection collection)
|
|
{
|
|
if (collection == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
StringBuilder query = new StringBuilder();
|
|
foreach (string key in collection.Keys)
|
|
{
|
|
query.AppendFormat("&{0}={1}", key, collection[key] ?? "");
|
|
}
|
|
|
|
return query.Length > 0 ? query.Remove(0, 1).ToString() : string.Empty;
|
|
}
|
|
|
|
public static void ForEach<T>(this IEnumerable<T> @this, Action<T> action)
|
|
{
|
|
T[] array = @this.ToArray();
|
|
foreach (T t in array)
|
|
{
|
|
action(t);
|
|
}
|
|
}
|
|
|
|
public static void ForEach<T>(this IEnumerable<T> @this, Action<T, int> action)
|
|
{
|
|
if (@this == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
T[] array = @this.ToArray();
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
action(array[i], i);
|
|
}
|
|
}
|
|
}
|
|
}
|