// recursive method public static MemberBinding[] AssembleSelectMembers <TEntity, TModel>( List <PropertyInfo> modelProperties, List <MemberBinding> memberBindings, ParameterExpression parameter) { if (modelProperties.Count > 0) { PropertyInfo property = modelProperties.First(); modelProperties.RemoveAt(0); List <string> entityPropertyPath = ObjectInspector.RetrieveGridEntityPropertyPath(property); // initiate recursive method Expression entityProperty = AssembleProperty( Expression.MakeMemberAccess(parameter, typeof(TEntity).GetProperty(entityPropertyPath.First())), entityPropertyPath.Skip(1).ToList()); MemberAssignment modelPropertyBind = Expression.Bind( typeof(TModel).GetProperty(property.Name), entityProperty); memberBindings.Add(modelPropertyBind); return(AssembleSelectMembers <TEntity, TModel>(modelProperties, memberBindings, parameter)); } return(memberBindings.ToArray()); }
public static Expression TransformDateString(Expression entityProperty) { UnaryExpression nullableEntityDate = Expression.Convert(entityProperty, typeof(DateTime?)); MethodInfo concatStringMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) }); MethodInfo dateNameMethod = ObjectInspector.GetSqlFunctionDateTimeMethodInfo("DateName"); var datePart = Expression.Call(dateNameMethod, Expression.Constant("dd"), nullableEntityDate); Expression transformPredicate = Expression.Add(datePart, Expression.Constant("/"), concatStringMethod); MethodInfo datePartMethod = ObjectInspector.GetSqlFunctionDateTimeMethodInfo("DatePart"); MethodInfo stringConvertMethod = ObjectInspector.GetSqlFunctionStringConvertMethod(typeof(double?)); // original month from 'MM' gives the name of the month. Therefore, do SqlFunctions.StringConvert((double)'MM') // to extract digits datePart = Expression.Call( stringConvertMethod, Expression.Convert(Expression.Call(datePartMethod, Expression.Constant("MM"), nullableEntityDate), typeof(Nullable <double>))); transformPredicate = Expression.Add(transformPredicate, datePart, concatStringMethod); transformPredicate = Expression.Add(transformPredicate, Expression.Constant("/"), concatStringMethod); datePart = Expression.Call(dateNameMethod, Expression.Constant("yyyy"), nullableEntityDate); transformPredicate = Expression.Add(transformPredicate, datePart, concatStringMethod); return(transformPredicate); }
public static Expression <Func <TEntity, TModel> > GetSelectClause <TEntity, TModel>() { ParameterExpression parameter = Expression.Parameter(typeof(TEntity), "entity"); List <PropertyInfo> properties = ObjectInspector.ExtractGridModelSelectProperties <TModel>(); MemberBinding[] modelMembers = AssembleSelectMembers <TEntity, TModel>(properties, new MemberBinding[] { }.ToList(), parameter); NewExpression newModelItem = Expression.New(typeof(TModel)); MemberInitExpression init = Expression.MemberInit(newModelItem, modelMembers); return((Expression <Func <TEntity, TModel> >)Expression.Lambda(init, parameter)); }
public static Expression TransformTimeString(Expression entityProperty) { UnaryExpression nullableEntityTime = Expression.Convert(entityProperty, typeof(TimeSpan?)); MethodInfo concatStringMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) }); MethodInfo dateNameMethod = ObjectInspector.GetSqlFunctionDateTimeMethodInfo("DateName"); var datePart = Expression.Call(dateNameMethod, Expression.Constant("hh"), nullableEntityTime); Expression transformPredicate = Expression.Add(datePart, Expression.Constant(":"), concatStringMethod); datePart = Expression.Call(dateNameMethod, Expression.Constant("mi"), nullableEntityTime); transformPredicate = Expression.Add(transformPredicate, datePart, concatStringMethod); transformPredicate = Expression.Add(transformPredicate, Expression.Constant(":"), concatStringMethod); datePart = Expression.Call(dateNameMethod, Expression.Constant("ss"), nullableEntityTime); transformPredicate = Expression.Add(transformPredicate, datePart, concatStringMethod); return(transformPredicate); }