Skip to content

Instantly share code, notes, and snippets.

@skoon
Created December 6, 2012 06:49
Show Gist options
  • Save skoon/4222288 to your computer and use it in GitHub Desktop.
Save skoon/4222288 to your computer and use it in GitHub Desktop.
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment