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
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Tango.Core;
/// <summary>
/// Contains <see cref="IEnumerable{T}"/> extension methods.
/// </summary>
public static class IEnumerableExtensions
{
/// <summary>
/// Creates a new observable collection from the specified enumerable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <returns></returns>
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerable)
{
return new ObservableCollection<T>(enumerable);
}
/// <summary>
/// Creates a new synchronized observable collection from the specified enumerable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <returns></returns>
public static SynchronizedObservableCollection<T> ToSynchronizedObservableCollection<T>(this IEnumerable<T> enumerable)
{
return new SynchronizedObservableCollection<T>(enumerable);
}
/// <summary>
/// Creates a new read-only collection from the specified enumerable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <returns></returns>
public static ReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> enumerable)
{
return new ReadOnlyCollection<T>(enumerable.ToList());
}
/// <summary>
/// Replace an element in the array.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items">The items.</param>
/// <param name="condition">The condition.</param>
/// <param name="replaceAction">The replace action.</param>
/// <returns></returns>
public static IEnumerable<T> Replace<T>(this IEnumerable<T> items, Predicate<T> condition, Func<T, T> replaceAction)
{
return items.Select(item => condition(item) ? replaceAction(item) : item);
}
/// <summary>
/// Returns the closest double number in the array.
/// </summary>
/// <param name="items">The items.</param>
/// <param name="source">The source.</param>
/// <returns></returns>
public static double Closest(this IEnumerable<double> items, double source)
{
double closest = items.Aggregate((x, y) => Math.Abs(x - source) < Math.Abs(y - source) ? x : y);
return closest;
}
/// <summary>
/// Takes the last n elements from the list..
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="N">The n.</param>
/// <returns></returns>
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int N)
{
return source.Skip(Math.Max(0, source.Count() - N));
}
/// <summary>
/// Joins the list items ToString outputs to a single string using the specified separator.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="separator">The separator.</param>
/// <returns></returns>
public static String Join<T>(this IEnumerable<T> source, String separator)
{
return String.Join(separator, source);
}
/// <summary>
/// Returns a distinct collection by the specified property.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="property">The property.</param>
/// <returns></returns>
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> property)
{
return source.GroupBy(property).Select(g => g.First());
}
/// <summary>
/// Orders the collection by natural alphanumeric string.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="selector">The selector.</param>
/// <returns></returns>
public static IOrderedEnumerable<T> OrderByAlphaNumeric<T>(this IEnumerable<T> source, Func<T, string> selector)
{
int max = source
.SelectMany(i => Regex.Matches(selector(i), @"\d+").Cast<Match>().Select(m => (int?)m.Value.Length))
.Max() ?? 0;
return source.OrderBy(i => Regex.Replace(selector(i), @"\d+", m => m.Value.PadLeft(max, '0')));
}
}
|