2
using System.Collections.Generic;
4
namespace ServiceStack.Common
6
public static class EnumerableExtensions
8
public static bool IsEmpty<T>(this ICollection<T> collection)
10
return collection == null || collection.Count == 0;
13
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> items)
15
return new HashSet<T>(items);
18
public static List<To> SafeConvertAll<To, From>(this IEnumerable<From> items, Func<From, To> converter)
20
return items == null ? new List<To>() : Extensions.EnumerableExtensions.ConvertAll(items, converter);
23
public static List<object> ToObjects<T>(this IEnumerable<T> items)
25
var to = new List<object>();
26
foreach (var item in items)
33
public static string FirstNonDefaultOrEmpty(this IEnumerable<string> values)
35
foreach (var value in values)
37
if (!string.IsNullOrEmpty(value)) return value;
42
public static T FirstNonDefault<T>(this IEnumerable<T> values)
44
foreach (var value in values)
46
if (!Equals(value, default(T))) return value;
51
public static bool EquivalentTo<T>(this IEnumerable<T> thisList, IEnumerable<T> otherList)
53
if (thisList == null || otherList == null) return thisList == otherList;
55
var otherEnum = otherList.GetEnumerator();
56
foreach (var item in thisList)
58
if (!otherEnum.MoveNext()) return false;
60
var thisIsDefault = Equals(item, default(T));
61
var otherIsDefault = Equals(otherEnum.Current, default(T));
62
if (thisIsDefault || otherIsDefault)
64
return thisIsDefault && otherIsDefault;
67
if (!item.Equals(otherEnum.Current)) return false;
69
var hasNoMoreLeftAsWell = !otherEnum.MoveNext();
70
return hasNoMoreLeftAsWell;
73
public static IEnumerable<T[]> BatchesOf<T>(this IEnumerable<T> sequence, int batchSize)
75
var batch = new List<T>(batchSize);
76
foreach (var item in sequence)
79
if (batch.Count >= batchSize)
81
yield return batch.ToArray();
88
yield return batch.ToArray();
93
public static Dictionary<TKey, T> ToSafeDictionary<T, TKey>(this IEnumerable<T> list, Func<T, TKey> expr)
95
var map = new Dictionary<TKey, T>();
98
foreach (var item in list)
100
map[expr(item)] = item;
b'\\ No newline at end of file'