60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace RPGCoreCommon.Helpers
|
|
{
|
|
public static class LinqExtensions
|
|
{
|
|
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
|
{
|
|
foreach (var element in source) action.Invoke(element);
|
|
return source;
|
|
}
|
|
|
|
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<int, T> action)
|
|
{
|
|
var index = 0;
|
|
foreach (var element in source) action(index++, element);
|
|
return source;
|
|
}
|
|
|
|
public static IEnumerable<T> Sort<T>(this IEnumerable<T> source, Comparison<T> comparison)
|
|
{
|
|
var list = source.ToList();
|
|
list.Sort(comparison);
|
|
return list;
|
|
}
|
|
|
|
public static IEnumerable<T> Sort<T>(this IEnumerable<T> source)
|
|
{
|
|
return source.OrderBy(x => x);
|
|
}
|
|
|
|
public static IEnumerable<T> SortDescending<T>(this IEnumerable<T> source)
|
|
{
|
|
return source.OrderByDescending(x => x);
|
|
}
|
|
|
|
public static IEnumerable<TResult> DictSelect<TKey, TValue, TResult>(this IDictionary<TKey, TValue> source, Func<TKey, TValue, TResult> func)
|
|
{
|
|
foreach (var pair in source) yield return func(pair.Key, pair.Value);
|
|
}
|
|
|
|
public static IDictionary<TKey,TValue> DictForEach<TKey, TValue>(this IDictionary<TKey,TValue> source, Action<TKey, TValue> action)
|
|
{
|
|
foreach (var pair in source) action(pair.Key, pair.Value);
|
|
return source;
|
|
}
|
|
|
|
public static string StringJoin(this IEnumerable<string> source, string separator)
|
|
{
|
|
return string.Join(separator, source);
|
|
}
|
|
|
|
public static IEnumerable<T> AppendIf<T>(this IEnumerable<T> source, T element, bool condition)
|
|
{
|
|
return condition ? source.Append(element) : source;
|
|
}
|
|
}
|
|
} |