Skip to content

Instantly share code, notes, and snippets.

@dodbrian
Last active February 2, 2018 09:34
Show Gist options
  • Save dodbrian/bf650817077df98c58e1c6769ad65310 to your computer and use it in GitHub Desktop.
Save dodbrian/bf650817077df98c58e1c6769ad65310 to your computer and use it in GitHub Desktop.
Convert string column name into property accessor
public Expression<Func<TEntity, TProperty>> ColumnByName<TEntity, TProperty>(string columnName)
{
var propInfo = typeof(TEntity).GetProperty(columnName);
var parameter = Expression.Parameter(typeof(TEntity));
var memberAccess = Expression.MakeMemberAccess(parameter, propInfo);
var exp = Expression.Lambda<Func<TEntity, TProperty>>(memberAccess, parameter);
return exp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment