From 660fa2c8159bb8adf7b7f0be5fa72ab1e541feac Mon Sep 17 00:00:00 2001 From: David Hall Date: Mon, 26 Mar 2018 13:17:25 -0600 Subject: [PATCH] Added OfType, OrderByDescending, Reverse, Take and TakeWhile extension methods. --- Core/BkwdComp/Linq.NET2.cs | 74 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/Core/BkwdComp/Linq.NET2.cs b/Core/BkwdComp/Linq.NET2.cs index 65c3ada4..9c6ec867 100644 --- a/Core/BkwdComp/Linq.NET2.cs +++ b/Core/BkwdComp/Linq.NET2.cs @@ -80,7 +80,7 @@ namespace System.Linq /// Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty. /// The type of the elements of . /// The sequence to return the specified value for if it is empty. - /// The value to return if the sequence is empty. This value defaults to . + /// The value to return if the sequence is empty. This value defaults to default(TSource). /// An that contains if source is empty; otherwise, source. public static IEnumerable DefaultIfEmpty(this IEnumerable source, TSource defaultValue = default(TSource)) { @@ -254,6 +254,19 @@ namespace System.Linq throw new InvalidOperationException("No elements"); } + /// Filters the elements of an based on a specified type. + /// The type to filter the elements of the sequence on. + /// The whose elements to filter. + /// An that contains elements from the input sequence of type . + public static IEnumerable OfType(this IEnumerable source) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + foreach (object obj in source) + { + if (obj is TResult) yield return (TResult)obj; + } + } + /// Sorts the elements of a sequence in ascending order according to a key. /// The type of the elements of . /// The type of the key returned by . @@ -268,6 +281,31 @@ namespace System.Linq return d.Values; } + /// Sorts the elements of a sequence in descending order. + /// The type of the elements of . + /// The type of the key returned by . + /// A sequence of values to order. + /// A function to extract a key from an element. + /// An whose elements are sorted in descending order according to a key. + public static IEnumerable OrderByDescending(this IEnumerable source, Func keySelector) + { + var d = new SortedDictionary(); + foreach (var item in source) + d.Add(keySelector(item), item); + return d.Values.Reverse(); + } + + /// Inverts the order of the elements in a sequence. + /// The type of the elements of source. + /// A sequence of values to reverse. + /// A sequence whose elements correspond to those of the input sequence in reverse order. + public static IEnumerable Reverse(this IEnumerable source) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + IList items = source as IList ?? source.ToList(); + for (int i = items.Count - 1; i >= 0; i--) yield return items[i]; + } + /// Projects each element of a sequence into a new form. /// The type of the elements of . /// The type of the value returned by . @@ -326,6 +364,40 @@ namespace System.Linq /// The sum of the projected values. public static int Sum(this IEnumerable source, Func selector) => Sum(Select(source, selector)); + /// Returns a specified number of contiguous elements from the start of a sequence. + /// The type of the elements of source. + /// A sequence to return elements from. + /// The number of elements to return. + /// An that contains the specified number of elements from the start of the input sequence. + public static IEnumerable Take(this IEnumerable source, int count) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (count > 0) + { + foreach (TSource element in source) + { + yield return element; + if (--count == 0) break; + } + } + } + + /// Returns elements from a sequence as long as a specified condition is true. + /// The type of the elements of source. + /// A sequence to return elements from. + /// A function to test each element for a condition. + /// An that contains the elements from the input sequence that occur before the element at which the test no longer passes. + public static IEnumerable TakeWhile(this IEnumerable source, Func predicate) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (predicate == null) throw new ArgumentNullException(nameof(predicate)); + foreach (TSource element in source) + { + if (!predicate(element)) break; + yield return element; + } + } + /// Creates an array from a . /// The type of the elements of . /// An to create an array from.