Skip to main content

LinqExtensions

Provides generic LINQ extension methods.

public static class LinqExtensions

Inheritance ObjectLinqExtensions

Methods

GetRangeEnumerable<T>(IReadOnlyList<T>, Int32, Int32)

Retrieves a range of items from the list, via zero-based indexing

public static IEnumerable<T> GetRangeEnumerable<T>(IReadOnlyList<T> list, int start, int end)

Type Parameters

T

Parameters

list IReadOnlyList<T>

start Int32
Zero-based index at which the range starts

end Int32
Non-inclusive zero-based index at which the range ends

Returns

IEnumerable<T>
The range of items

Exceptions

ArgumentException
and do not denote a valid range of items in the list

ToJsonString<T>(IEnumerable<T>)

Shorthand method to serialize as JSON a collection of objects

public static string ToJsonString<T>(IEnumerable<T> items)

Type Parameters

T

Parameters

items IEnumerable<T>

Returns

String

WithIndex<T>(IEnumerable<T>)

Associates each element with its index. Handy shortcut for foreach loops.

public static IEnumerable<ValueTuple<T, int>> WithIndex<T>(IEnumerable<T> items)

Type Parameters

T

Parameters

items IEnumerable<T>

Returns

IEnumerable<ValueTuple<T, Int32>>

AppendLines(StringBuilder, IEnumerable<String>)

Append all lines to the StringBuilder

public static void AppendLines(StringBuilder sb, IEnumerable<string> lines)

Parameters

sb StringBuilder

lines IEnumerable<String>

OrderedDistinct<TSource>(IOrderedEnumerable<TSource>)

Optimized Distinct() which assumes the items are ordered by their Equals(Object) method

note

The result is NOT correct if items are ordered via any other comparer

public static IEnumerable<TSource> OrderedDistinct<TSource>(IOrderedEnumerable<TSource> items)

Type Parameters

TSource

Parameters

items IOrderedEnumerable<TSource>
The items ordered via their default comparer, i.e. the Equals(Object) method

Returns

IEnumerable<TSource>

OrderedDistinct<TSource, TKey>(IOrderedEnumerable<TSource>, Func<TSource, TKey>)

Optimized Distinct() which assumes the items are already ordered according to the given key

note

The result is NOT correct if items are ordered via any other key or comparer

public static IEnumerable<TSource> OrderedDistinct<TSource, TKey>(IOrderedEnumerable<TSource> items, Func<TSource, TKey> keySelector)

Type Parameters

TSource

TKey

Parameters

items IOrderedEnumerable<TSource>
The sequence ordered via the given key

keySelector Func<TSource, TKey>
A function to extract a key from an element. Must be the same key the items have been ordered with.

Returns

IEnumerable<TSource>