Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extension method for materializing a LINQ method chain to an Array or List without resizing. Mentioned here: https://stefanolsen.com/posts/8-things-to-avoid-to-make-an-episerver-site-go-faster/
// Copyright 2019 Stefan Holm Olsen
//
// Permission to use, copy, modify, and/or distribute this software for an
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
// OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
using System;
using System.Collections.Generic;
public static class CollectionExtensions
{
/// <summary>
/// Creates an array from a IEnumerable<T>.
/// </summary>
/// <example>
/// <code>
/// var pagedList = list.Take(10).ToArray(10);
/// var viewModels = list.Select(CreateViewModel).ToArray(list.Count);
/// </code>
/// </example>
/// <exception cref="System.ArgumentNullException">source is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">capacity is 0 or less.</exception>
/// <returns>An array that contains the elements from the input sequence.</returns>
/// <param name="source">An IEnumerable<T> to create an array from.</param>
/// <param name="length">The number of items that should be in the array.</param>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
public static TSource[] ToArray<TSource>(
this IEnumerable<TSource> source,
int length)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (length <= 0)
{
throw new ArgumentOutOfRangeException(nameof(length), length, null);
}
TSource[] result = new TSource[length];
int i = 0;
foreach (TSource source1 in source)
{
result[i] = source1;
if (i++ >= length)
{
break;
}
}
return result;
}
/// <summary>
/// Creates a List<T> from a IEnumerable<T>.
/// </summary>
/// <example>
/// <code>
/// var pagedList = list.Take(10).ToList(10);
/// var viewModels = list.Select(CreateViewModel).ToList(list.Count);
/// </code>
/// </example>
/// <exception cref="System.ArgumentNullException">source is null.</exception>
/// <exception cref="System.ArgumentOutOfRangeException">capacity is 0 or less.</exception>
/// <returns>A List<T> that contains the elements from the input sequence.</returns>
/// <param name="source">An IEnumerable<T> to create a List<T> from.</param>
/// <param name="length">The number of items that should be in the list.</param>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
public static List<TSource> ToList<TSource>(
this IEnumerable<TSource> source,
int capacity)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity), capacity, null);
}
List<TSource> result = new List<TSource>(capacity);
foreach (TSource source1 in source)
{
result.Add(source1);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.