Last active
November 5, 2022 19:57
-
-
Save JustinStolle/1e897f55fa93eaf1a25f8980f4185eef to your computer and use it in GitHub Desktop.
Creates an IEnumerable<SelectListItem> from an IEnumerable<T>.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------- | |
// <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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create an IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create a IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create a IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create a IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create a IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create a IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create a IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create a IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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<SelectListItem> from an IEnumerable<T>. | |
/// </summary> | |
/// <typeparam name="T">The type of the elements of enumerable.</typeparam> | |
/// <param name="enumerable">The IEnumerable<T> to create a IEnumerable<SelectListItem> 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<SelectListItem> that contains elements from the IEnumerable<T>.</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