private static DTOLambdaAndType GenerateSelector <TEntity>(String propertyName) where TEntity : class { DTOLambdaAndType resultset = new DTOLambdaAndType(); var parameter = Expression.Parameter(typeof(TEntity), "Entity"); PropertyInfo property; Expression propertyAccess; if (propertyName.Contains('.')) { String[] childProperties = propertyName.Split('.'); property = typeof(TEntity).GetProperty(childProperties[0], BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); propertyAccess = Expression.MakeMemberAccess(parameter, property); for (int i = 1; i < childProperties.Length; i++) { property = property.PropertyType.GetProperty(childProperties[i], BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); propertyAccess = Expression.MakeMemberAccess(propertyAccess, property); } } else { property = typeof(TEntity).GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); propertyAccess = Expression.MakeMemberAccess(parameter, property); } resultset.resultType = property.PropertyType; resultset.lambdaExpression = Expression.Lambda(propertyAccess, parameter); return(resultset); }
private static MethodCallExpression GenerateMethodCall <TEntity>(IQueryable <TEntity> source, string methodName, String fieldName) where TEntity : class { MethodCallExpression resultExp = null; Type type = typeof(TEntity); Type selectorResultType; DTOLambdaAndType result = new DTOLambdaAndType(); result = GenerateSelector <TEntity>(fieldName); LambdaExpression selector = result.lambdaExpression; selectorResultType = result.resultType; resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { type, selectorResultType }, source.Expression, Expression.Quote(selector)); return(resultExp); }