public static Func <T, T> GenerateSelectedColumns <T> (string[] Fields = null) { string[] EntityFields; if (Fields == null) { EntityFields = typeof(T).GetProperties() .Where(d => d.GetGetMethod().IsStatic == false) .Select(propertyInfo => propertyInfo.Name) .ToArray(); } else { EntityFields = Fields; } var xParameter = Expression.Parameter(typeof(T), "o"); var xNew = Expression.New(typeof(T)); var bindings = EntityFields.Select(o => o.Trim()) .Select(o => { var mi = typeof(T).GetProperty(o); var xOriginal = Expression.Property(xParameter, mi); return(Expression.Bind(mi, xOriginal)); }); var xInit = Expression.MemberInit(xNew, bindings); var lambda = Expression.Lambda <Func <T, T> > (xInit, xParameter); return(lambda.Compile()); }
public static Func <T, T> DynamicSelectGenerator <T>(string Fields = "") { string[] EntityFields; if (Fields == "") { EntityFields = typeof(T).GetProperties().Select(propertyInfo => propertyInfo.Name).ToArray(); } else { EntityFields = Fields.Split(','); } var xParameter = Expression.Parameter(typeof(T), "o"); var xNew = Expression.New(typeof(T)); var bindings = EntityFields.Select(o => o.Trim()) .Select(o => { var mi = typeof(T).GetProperty(o); var xOriginal = Expression.Property(xParameter, mi); return(Expression.Bind(mi, xOriginal)); } ); var xInit = Expression.MemberInit(xNew, bindings); var lambda = Expression.Lambda <Func <T, T> >(xInit, xParameter); return(lambda.Compile()); }
public static Func <T, T> DynamicSelectGenerator <T>(string Fields = "") { string[] EntityFields; if (Fields == "") { // get Properties of the T EntityFields = typeof(T).GetProperties().Select(propertyInfo => propertyInfo.Name).ToArray(); } else { EntityFields = Fields.Split(','); } // input parameter "o" var xParameter = Expression.Parameter(typeof(T), "o"); // new statement "new Data()" var xNew = Expression.New(typeof(T)); // create initializers var bindings = EntityFields.Select(o => o.Trim()) .Select(o => { // property "Field1" var mi = typeof(T).GetProperty(o); // original value "o.Field1" var xOriginal = Expression.Property(xParameter, mi); // set value "Field1 = o.Field1" return(Expression.Bind(mi, xOriginal)); } ); // initialization "new Data { Field1 = o.Field1, Field2 = o.Field2 }" var xInit = Expression.MemberInit(xNew, bindings); // expression "o => new Data { Field1 = o.Field1, Field2 = o.Field2 }" var lambda = Expression.Lambda <Func <T, T> >(xInit, xParameter); // compile to Func<Data, Data> return(lambda.Compile()); }