Table of Contents

EnumerableExtensions Class

Definition

Namespace
Cuemon.Extensions.Collections.Generic
Assemblies
Cuemon.Extensions.Collections.Generic.dll
Source
src/Cuemon.Extensions.Collections.Generic/EnumerableExtensions.cs

Extension methods for the IEnumerable<T> interface.

public static class EnumerableExtensions
Inheritance
EnumerableExtensions

Examples

EnumerableExtensions provides extension methods for IEnumerable<T> including chunking, shuffling, ordering, pagination, and materialization. This example starts with an int[] { 4, 1, 3, 2 } and applies Chunk(2) to split into batches, Shuffle with both random and deterministic seeds, OrderAscending and OrderDescending for sorted output, RandomOrDefault for element selection, and Yield to wrap a single value. It also demonstrates ToDictionary from KeyValuePair sequences, ToPartitioner for partitioned iteration, and ToPagination/ToPaginationList for page-based access. Console output confirms each operation's results, such as chunk count (2), sorted order (1, 2, 3, 4), and dictionary value for key "beta".

using System;
using System.Collections.Generic;
using System.Linq;
using Cuemon.Extensions.Collections.Generic;
using CuemonEnumerableExtensions = Cuemon.Extensions.Collections.Generic.EnumerableExtensions;

namespace MyApp.Examples
{
    public static class EnumerableExtensionsExample
    {
        public static void Demonstrate()
        {
            var values = new[] { 4, 1, 3, 2 };
            var chunked = CuemonEnumerableExtensions.Chunk(values, 2).ToList();
            var shuffled = CuemonEnumerableExtensions.Shuffle(values).ToArray();
            var deterministicShuffle = CuemonEnumerableExtensions.Shuffle(values, (min, max) => min).ToArray();
            var ascending = values.OrderAscending().ToArray();
            var ascendingWithComparer = values.OrderAscending(Comparer<int>.Default).ToArray();
            var descending = CuemonEnumerableExtensions.OrderDescending(values).ToArray();
            var random = values.RandomOrDefault();
            var yielded = 5.Yield().Single();
            var dictionary = CuemonEnumerableExtensions.ToDictionary(new[]
            {
                new KeyValuePair<string, int>("alpha", 1),
                new KeyValuePair<string, int>("beta", 2)
            });
            var partitioner = values.ToPartitioner(2).ToList();
            var pagination = values.ToPagination(() => values.Length).ToList();
            var paginationList = values.ToPaginationList(() => values.Length);

            Console.WriteLine(chunked.Count);
            Console.WriteLine(shuffled.Length + deterministicShuffle.Length);
            Console.WriteLine(string.Join(", ", ascending));
            Console.WriteLine(string.Join(", ", ascendingWithComparer));
            Console.WriteLine(string.Join(", ", descending));
            Console.WriteLine(random);
            Console.WriteLine(yielded);
            Console.WriteLine(dictionary["beta"]);
            Console.WriteLine(partitioner.Count);
            Console.WriteLine(pagination.Count);
            Console.WriteLine(paginationList.Count);
        }
    }
}

Methods

Name Description
Chunk<T>(IEnumerable<T>, int)

Returns a chunked IEnumerable<T> sequence with a maximum of the specified size. Default is 128.

OrderAscending<T>(IEnumerable<T>)

Returns ascending sorted elements from a sequence by using the default comparer to compare values.

OrderAscending<T>(IEnumerable<T>, IComparer<T>)

Returns ascending sorted elements from a sequence by using a specified IComparer<T> to compare values.

OrderDescending<T>(IEnumerable<T>)

Returns descending sorted elements from a sequence by using the default comparer to compare values.

OrderDescending<T>(IEnumerable<T>, IComparer<T>)

Returns descending sorted elements from a sequence by using a specified IComparer<T> to compare values.

RandomOrDefault<T>(IEnumerable<T>)

Returns a random element of a sequence of elements, or a default value if no element is found.

Shuffle<T>(IEnumerable<T>)

Shuffles the specified source like a deck of cards.

Shuffle<T>(IEnumerable<T>, Func<int, int, int>)

Shuffles the specified source like a deck of cards.

ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>)

Creates a Dictionary<TKey, TValue> from the specified source sequence.

ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>)

Creates a Dictionary<TKey, TValue> from the specified source sequence.

ToPaginationList<T>(IEnumerable<T>, Func<int>, Action<PaginationOptions>)

Converts the specified source to an eagerly materialized generic and read-only pagination list.

ToPagination<T>(IEnumerable<T>, Func<int>, Action<PaginationOptions>)

Converts the specified source to a generic and read-only pagination sequence.

ToPartitioner<T>(IEnumerable<T>, int)

Extends the specified source to support iterating in partitions.

Yield<T>(T)

Returns an IEnumerable<T> sequence with the specified value as the only element.