-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumerableExtensions.cs
432 lines (391 loc) · 17.2 KB
/
EnumerableExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#region File info
// *********************************************************************************************************
// Funcular.ExtensionMethods>Funcular.ExtensionMethods>EnumerableExtensions.cs
// Created: 2015-06-26 3:03 PM
// Updated: 2016-07-10 10:46 AM
// By: Paul Smith
//
// *********************************************************************************************************
// LICENSE: The MIT License (MIT)
// *********************************************************************************************************
// Copyright (c) 2010-2015 <copyright holders>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// *********************************************************************************************************
#endregion
#region Usings
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
#endregion
namespace Funcular.ExtensionMethods
{
/// <summary>
/// Extension methods for IEnumerable objects.
/// </summary>
public static class EnumerableExtensions
{
/// <summary>
/// Safely determine if the sequence is non-null AND contains elements.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasContents<T>(this IEnumerable<T> enumerable)
{
return enumerable != null && enumerable.Any();
}
/// <summary>
/// Prevent null reference exceptions when referencing enumerables
/// by using x.OrEmpty().DoSomething().
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> OrEmpty<T>(this IEnumerable<T> enumerable)
{
return enumerable ?? Enumerable.Empty<T>();
}
/// <summary>
/// Method used to order an enumerable by the property's name as a string
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="source"></param>
/// <param name="orderByProperty">Name of property to sort by</param>
/// <param name="desc">Descending if true</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<TEntity> OrderBy<TEntity>(this IEnumerable<TEntity> source, string orderByProperty,
bool desc = false)
{
var command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);
var property = type.GetCachedProperty(orderByProperty);
var parameter = Expression.Parameter(type, "p");
if (property == null)
throw new ArgumentNullException("Unable to order by property named [" + orderByProperty + "]. Check to ensure this property exists in your IEnumerable.");
MemberExpression propertyAccess = Expression.MakeMemberAccess(parameter, property);
LambdaExpression orderByExpression = Expression.Lambda(propertyAccess, parameter);
TEntity[] array = source as TEntity[] ?? source.ToArray();
MethodCallExpression resultExpression = Expression.Call(typeof(Queryable), command, new[] { type, property.PropertyType },
array.AsQueryable().Expression, Expression.Quote(orderByExpression));
return array.AsQueryable().Provider.CreateQuery<TEntity>(resultExpression);
}
/// <summary>
/// Extension method to get a paged result set from an IEnumerable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="results"></param>
/// <param name="pageSize"></param>
/// <param name="page"></param>
/// <param name="sort"></param>
/// <param name="sortDir"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> GetPagedResults<T>(this IEnumerable<T> results, int pageSize, int page, string sort = "", string sortDir = "")
{
if (sort != "")
{
bool isAscendingSort = !(sortDir != null && sortDir.ToLower().Contains("desc"));
results = results.OrderBy(sort, isAscendingSort);
}
if (page > 1)
results = results.Skip((page - 1) * pageSize);
return results.Take(pageSize);
}
/// <summary>
/// Returns the enumerable member having the lowest value of <paramref name="selector" />
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source"></param>
/// <param name="selector"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TEntity MinBy<TEntity, TKey>(this IEnumerable<TEntity> source, Func<TEntity, TKey> selector) where TEntity : class
{
return source?.OrderBy(selector)
.FirstOrDefault();
}
/// <summary>
/// Returns the enumerable member having the highest value of <paramref name="selector" />
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source"></param>
/// <param name="selector"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TEntity MaxBy<TEntity, TKey>(this IEnumerable<TEntity> source, Func<TEntity, TKey> selector) where TEntity : class
{
return source?.OrderByDescending(selector)
.FirstOrDefault();
}
/// <summary>
/// Returns the enumerable member having the median value of <paramref name="selector" />
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source"></param>
/// <param name="selector"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TEntity MedianBy<TEntity, TKey>(this IEnumerable<TEntity> source, Func<TEntity, TKey> selector) where TEntity : class
{
if (source?.Any() != true)
return default(TEntity);
var array = source as TEntity[] ?? source.ToArray();
var count = array.Count();
var midpoint = (count / 2);
if (count%2 == 0)
midpoint--;
return array.OrderBy(selector).ElementAt(midpoint);
}
/// <summary>
/// Returns the median value of <paramref name="selector" /> among this Enumerable.
/// </summary>
/// <typeparam name="TColl"></typeparam>
/// <typeparam name="TValue">Must be assignable to decimal</typeparam>
/// <param name="source"></param>
/// <param name="selector"></param>
/// <returns></returns>
public static decimal? Median<TColl, TValue>(this IEnumerable<TColl> source, Func<TColl, TValue> selector)
{
return source.Select(selector).Median();
}
/// <summary>
/// Returns the median value of Enumerable, assuming <typeparamref name="T"></typeparamref>
/// can be cast to a decimal.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static decimal? Median<T>(this IEnumerable<T> source)
{
if (Nullable.GetUnderlyingType(typeof(T)) != null)
source = source.Where(x => x != null);
source = source.OrderBy(n => n);
T[] array = (source.ToArray());
int count = array.Count();
if (count == 0)
return null;
//source = array.OrderBy(n => n);
int midpoint = count / 2;
if (count % 2 == 0)
return (Convert.ToDecimal(array.ElementAt(midpoint - 1)) + Convert.ToDecimal(array.ElementAt(midpoint))) / 2.0M;
else
return Convert.ToDecimal(array.ElementAt(midpoint));
}
/// <summary>
/// Returns the first item encountered for each distinct selector value.
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source"></param>
/// <param name="selector"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector) where TSource : class
{
var keys = new HashSet<TKey>();
foreach (var item in source)
{
if (keys.Add(selector(item)))
yield return item;
}
}
/// <summary>
/// Supplies AddRange on ILists and ICollections, like Microsoft should have.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="originalCollection"></param>
/// <param name="addCollection"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ICollection<T> AddRange<T>(this ICollection<T> originalCollection, IEnumerable<T> addCollection)
{
foreach (var item in addCollection)
{
originalCollection.Add(item);
}
return originalCollection;
}
/// <summary>
/// Fluent syntax to enable constructs like
/// <example>myList.AddAnd(thing).Add(otherThing);</example>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static List<T> AddAnd<T>(this List<T> list, T item)
{
list.Add(item);
return list;
}
/// <summary>
/// Fluent syntax to enable constructs like
/// <example>myList.ClearAnd().Add(something);</example>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ICollection<T> ClearAnd<T>(this ICollection<T> list)
{
list.Clear();
return list;
}
/// <summary>
/// Clears a collection and replaces it with the supplied elements.
/// Useful for replacing readonly collections that you can't assign directly.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="originalCollection"></param>
/// <param name="replaceWith"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ICollection<T> ReplaceWith<T>(this ICollection<T> originalCollection, IEnumerable<T> replaceWith)
{
if (originalCollection == null)
return null;
originalCollection.Clear();
originalCollection.AddRange(replaceWith);
return originalCollection;
}
/// <summary>
/// Flattens any hierarchy of nodes and returns it as a collection.
/// All nodes will themselves have empty child item collections.
/// If id assignment expression is provided, nodes will have the
/// proper parent id assigned.
/// </summary>
public static IEnumerable<T> Flatten<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childSelector, Action<T,T> parentIdAssigner = null)
{
var stack = new Stack<T>();
foreach (var item in source)
{
stack.Push(item);
}
while (stack.Count > 0)
{
var current = stack.Pop();
yield return current;
foreach (var childItem in childSelector(current))
{
parentIdAssigner?.Invoke(childItem, current);
stack.Push(childItem);
}
}
}
/// <summary>
/// Returns the collection of values in the enumeration type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static List<T> EnumToList<T>()
{
Type enumType = typeof(T);
// Can't use type constraints on value types, so have to do check like this
if (enumType.BaseType != typeof(Enum))
throw new ArgumentException("T must be of type System.Enum");
return new List<T>(Enum.GetValues(enumType) as IEnumerable<T>);
}
/// <summary>
/// Yields all of the individual Enum values represented in this Flags instance.
/// Only valid for bitmask/Flags enums.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<Enum> GetFlags(this Enum value)
{
return getFlags(value, Enum.GetValues(value.GetType()).Cast<Enum>().ToArray());
}
/// <summary>
/// Use to decompose a Flags Enum *member* that itself is a bitwise combination
/// of other members of the same enum.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<Enum> GetIndividualFlags(this Enum value)
{
var flags = getFlags(value, getFlagValues(value.GetType()).ToArray());
return flags.Where(f => (Convert.ToInt32(f) & Convert.ToInt32(value)) != 0);
}
#region Nonpublic methods
private static IEnumerable<Enum> getFlags(Enum value, Enum[] values)
{
ulong bits = Convert.ToUInt64(value);
var results = new List<Enum>();
for (int i = values.Length - 1; i >= 0; i--)
{
ulong mask = Convert.ToUInt64(values[i]);
if (i == 0 && mask == 0L)
break;
if ((bits & mask) == mask)
{
results.Add(values[i]);
bits -= mask;
}
}
/* if (bits != 0L)
return Enumerable.Empty<Enum>();*/
if (Convert.ToUInt64(value) != 0L)
return results.Reverse<Enum>();
/*if (bits == Convert.ToUInt64(value) && values.Length > 0 && Convert.ToUInt64(values[0]) == 0L)
return values.Take(1);*/
return Enumerable.Empty<Enum>();
}
/// <summary>
/// Enumerates all of the values in a Flags (bitmasked) Enum.
/// </summary>
/// <param name="enumType"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IEnumerable<Enum> getFlagValues(Type enumType)
{
ulong flag = 0x1;
foreach (var value in Enum.GetValues(enumType).Cast<Enum>())
{
ulong bits = Convert.ToUInt64(value);
if (bits == 0L)
//yield return value;
continue; // skip the zero value
while (flag < bits)
flag <<= 1;
if (flag == bits)
yield return value;
}
}
#endregion
}
}