Skip to content

Instantly share code, notes, and snippets.

@JustinStolle
Last active November 5, 2022 19:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinStolle/1e897f55fa93eaf1a25f8980f4185eef to your computer and use it in GitHub Desktop.
Save JustinStolle/1e897f55fa93eaf1a25f8980f4185eef to your computer and use it in GitHub Desktop.
Creates an IEnumerable<SelectListItem> from an IEnumerable<T>.
//-----------------------------------------------------------------------
// <copyright file="ToSelectList.cs">
// Copyright Justin C. Stolle. All rights reserved.
// Licensed under the MIT License.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
/// <summary>
/// Partial class for helper methods
/// </summary>
public static partial class Helpers
{
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create an IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="selectAll">Whether all values are selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, bool selectAll = false) =>
enumerable.ToSelectList(value, value, selectAll);
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create a IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="selectedValue">Items matching this value will be initially selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, object selectedValue) =>
enumerable.ToSelectList(value, value, ToObjectEnumerable(selectedValue));
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create a IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="selectedValues">Items matching these values will be initially selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, IEnumerable<object> selectedValues) =>
enumerable.ToSelectList(value, value, selectedValues);
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create a IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="text">A function to extract display text from each element.</param>
/// <param name="selectAll">Whether all values are selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, Func<T, object> text, bool selectAll = false)
{
foreach (var f in enumerable.Where(x => x != null))
{
yield return new SelectListItem()
{
Value = value(f).ToString(),
Text = text(f).ToString(),
Selected = selectAll
};
}
}
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create a IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="text">A function to extract display text from each element.</param>
/// <param name="selectedValue">Items matching this value will be initially selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, Func<T, object> text, object selectedValue) =>
enumerable.ToSelectList(value, text, ToObjectEnumerable(selectedValue));
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create a IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="text">A function to extract display text from each element.</param>
/// <param name="selectedValues">Items matching these values will be initially selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, Func<T, object> text, IEnumerable<object> selectedValues)
{
var sel = selectedValues != null
? selectedValues.Where(x => x != null).ToList().ConvertAll<string>(x => x.ToString())
: new List<string>();
foreach (var f in enumerable.Where(x => x != null))
{
yield return new SelectListItem()
{
Value = value(f).ToString(),
Text = text(f).ToString(),
Selected = sel.Contains(value(f).ToString())
};
}
}
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create a IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="text">A function to extract display text from each element.</param>
/// <param name="group">A function to extract option group name from each element.</param>
/// <param name="selectAll">Whether all values are selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, Func<T, object> text, Func<T, object> group, bool selectAll = false)
{
var groups = enumerable.Select(x => group(x).ToString()).Distinct().Select(x => new SelectListGroup() { Name = x });
foreach (var g in groups)
{
foreach (var f in enumerable.Where(x => x != null))
{
if (g.Name.Equals(group(f).ToString(), StringComparison.CurrentCulture))
{
yield return new SelectListItem()
{
Value = value(f).ToString(),
Text = text(f).ToString(),
Selected = selectAll,
Group = g
};
}
}
}
}
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create a IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="text">A function to extract display text from each element.</param>
/// <param name="group">A function to extract option group name from each element.</param>
/// <param name="selectedValue">Items matching this value will be initially selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, Func<T, object> text, Func<T, object> group, object selectedValue) =>
enumerable.ToSelectList(value, text, group, ToObjectEnumerable(selectedValue));
/// <summary>
/// Creates an IEnumerable&lt;SelectListItem&gt; from an IEnumerable&lt;T&gt;.
/// </summary>
/// <typeparam name="T">The type of the elements of enumerable.</typeparam>
/// <param name="enumerable">The IEnumerable&lt;T&gt; to create a IEnumerable&lt;SelectListItem&gt; from.</param>
/// <param name="value">A function to extract a value from each element.</param>
/// <param name="text">A function to extract display text from each element.</param>
/// <param name="group">A function to extract option group name from each element.</param>
/// <param name="selectedValues">Items matching these values will be initially selected.</param>
/// <returns>An IEnumerable&lt;SelectListItem&gt; that contains elements from the IEnumerable&lt;T&gt;.</returns>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, object> value, Func<T, object> text, Func<T, object> group, IEnumerable<object> selectedValues)
{
var sel = selectedValues != null
? selectedValues.Where(x => x != null).ToList().ConvertAll<string>(x => x.ToString())
: new List<string>();
var groups = enumerable.Select(x => group(x).ToString()).Distinct().Select(x => new SelectListGroup() { Name = x });
foreach (var g in groups)
{
foreach (var f in enumerable.Where(x => x != null))
{
if (g.Name.Equals(group(f).ToString(), StringComparison.CurrentCulture))
{
yield return new SelectListItem()
{
Value = value(f).ToString(),
Text = text(f).ToString(),
Selected = sel.Contains(value(f).ToString()),
Group = g
};
}
}
}
}
/// <summary>
/// Returns an object as an IEnumerable list of objects
/// </summary>
/// <param name="o">An object</param>
/// <returns>An IEnumerable list of objects</returns>
private static IEnumerable<object> ToObjectEnumerable(object o) => new List<object>()
{
o
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment