Skip to content

Instantly share code, notes, and snippets.

@skoon
Created December 6, 2012 06:49

Revisions

  1. Scott Koon created this gist Dec 6, 2012.
    25 changes: 25 additions & 0 deletions ListAllTheThings.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    public static class Extensions
    {
    public static IEnumerable<DropDownFormat> ToDropDownFormat<T>(this IList<T> list,
    Expression<Func<T, int>> idColumn,
    Expression<Func<T, string>> textColumn)
    {
    List<DropDownFormat> retList = new List<DropDownFormat>();

    var idMemberExp = (MemberExpression)idColumn.Body;
    var textMemberExp = (MemberExpression)textColumn.Body;
    var idName = idMemberExp.Member.Name;
    var textName = textMemberExp.Member.Name;

    foreach (T o in list)
    {
    var typeOfList = typeof(T);
    var id = (int)typeOfList.GetProperty(idName).GetValue(o, null);
    var text = (string)typeOfList.GetProperty(textName).GetValue(o, null);
    var ddf = new DropDownFormat() { Id = id, Text = text };
    retList.Add(ddf);
    }
    return retList.ToList();
    }

    }