コード例 #1
0
        // TODO: Check if the ThenBy portion of this works
        public static Func <IQueryable, IQueryable> BuildOrderByFunc(bool isThenBy, Type elementType, OrderByClause.OrderByItem obi)
        {
            var        propertyPath  = obi.PropertyPath;
            bool       isDesc        = obi.IsDesc;
            var        paramExpr     = Expression.Parameter(elementType, "o");
            Expression nextExpr      = paramExpr;
            var        propertyNames = propertyPath.Split('/').ToList();

            propertyNames.ForEach(pn => {
                var nextElementType = nextExpr.Type;
                var propertyInfo    = nextElementType.GetTypeInfo().GetProperty(pn);
                if (propertyInfo == null)
                {
                    throw new Exception("Unable to locate property: " + pn + " on type: " + nextElementType.ToString());
                }
                nextExpr = Expression.MakeMemberAccess(nextExpr, propertyInfo);
            });
            var lambdaExpr = Expression.Lambda(nextExpr, paramExpr);

            var orderByMethod = GetOrderByMethod(isThenBy, isDesc, elementType, nextExpr.Type);

            var baseType = isThenBy ? typeof(IOrderedQueryable <>) : typeof(IQueryable <>);
            var func     = BuildIQueryableFunc(elementType, orderByMethod, lambdaExpr, baseType);

            return(func);
        }
コード例 #2
0
        // TODO: Check if the ThenBy portion of this works
        private static Func <IQueryable, IQueryable> BuildOrderByFunc(bool isThenBy, Type elementType, OrderByClause.OrderByItem obi)
        {
            bool       isDesc    = obi.IsDesc;
            var        paramExpr = Expression.Parameter(elementType, "o");
            Expression nextExpr  = paramExpr;

            #region Modified code - Used Property.Properties instead of PropertyPath
            obi.Property.Properties.ForEach(propertyInfo => {
                nextExpr = Expression.MakeMemberAccess(nextExpr, propertyInfo);
            });
            #endregion
            var lambdaExpr = Expression.Lambda(nextExpr, paramExpr);

            var orderByMethod = GetOrderByMethod(isThenBy, isDesc, elementType, nextExpr.Type);

            var baseType = isThenBy ? typeof(IOrderedQueryable <>) : typeof(IQueryable <>);
            var func     = BuildIQueryableFunc(elementType, orderByMethod, lambdaExpr, baseType);
            return(func);
        }