Skip to content

Instantly share code, notes, and snippets.

@actaneon
Created January 28, 2010 07:02
Show Gist options
  • Save actaneon/288509 to your computer and use it in GitHub Desktop.
Save actaneon/288509 to your computer and use it in GitHub Desktop.
List extensions useful for MVC
using System;
using System.Collections.Generic;
using System.Web.Mvc;
public static class ListExtensions
{
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T> action)
{
foreach (var item in collection) action(item);
return collection;
}
public static SelectList ToSelectList<T>(this IEnumerable<T> collection)
{
return new SelectList(collection, "Key", "Value");
}
public static SelectList ToSelectList<T>(this IEnumerable<T> collection, string selectedValue)
{
return new SelectList(collection, "Key", "Value", selectedValue);
}
public static SelectList ToSelectList<T>(this IEnumerable<T> collection,
string dataValueField, string dataTextField)
{
return new SelectList(collection, dataValueField, dataTextField);
}
public static SelectList ToSelectList<T>(this IEnumerable<T> collection,
string dataValueField, string dataTextField, string selectedValue)
{
return new SelectList(collection, dataValueField, dataTextField, selectedValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment