Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pksorensen/fc2d604594e938aa841e1dd1ef7d0982 to your computer and use it in GitHub Desktop.
Save pksorensen/fc2d604594e938aa841e1dd1ef7d0982 to your computer and use it in GitHub Desktop.
var columnSet = ColumnSetBuilder.Generate<msnfp_Transaction>(x => x.msnfp_TransactionId, x=>x.msnfp_Amount);
var columnSet2 = ColumnSetBuilder.Generate<msnfp_Transaction>(x =>new { x.msnfp_TransactionId, x.msnfp_Amount });
public static class ColumnSetBuilder
{
public static ColumnSet Generate<T>(params Expression<Func<T,object>>[] expressions)
{
return new ColumnSet(expressions.Select(k => ParseExpression(k)).ToArray());
}
public static ColumnSet Generate<T>(Expression<Func<T, object>> expression)
{
if(expression.Body is NewExpression newExpression)
{
return new ColumnSet(newExpression.Arguments.OfType<MemberExpression>().Select(m => m.Member.GetCustomAttributes<AttributeLogicalNameAttribute>().FirstOrDefault().LogicalName).ToArray());
}
if (expression.Body is UnaryExpression)
{
return new ColumnSet(ParseExpression(expression));
}
throw new NotImplementedException();
}
private static string ParseExpression<T>(Expression<Func<T, object>> k)
{
if (k.Body is UnaryExpression unary)
{
return (unary.Operand as MemberExpression).Member.GetCustomAttributes<AttributeLogicalNameAttribute>().FirstOrDefault().LogicalName;
}
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment