Example #1
0
        public IQueryable <TEntity> QueryTable <TEntity>(QueryExpression queryExpression)
        {
            try
            {
                IQueryable <TEntity> result = null;

                var qTables = GetQueryTable <TEntity>(queryExpression);
                if (queryExpression.Include != null)
                {
                    for (int i = 0; i < queryExpression.Include.Length; i++)
                    {
                        qTables = qTables.Include(queryExpression.Include[i]);
                    }
                }
                Type EntityType = typeof(TEntity);

                Expression pred = QueryExpressionHelper.CovertToExpression(EntityType, queryExpression);
                if (pred == null)
                {
                    result = qTables;
                }
                else
                {
                    Expression expr = Expression.Call(typeof(Queryable), "Where", new Type[] { EntityType }, Expression.Constant(qTables), pred);
                    //Expression.Call(typeof(Queryable), "OrderBy",null,
                    result = qTables.AsQueryable().Provider.CreateQuery <TEntity>(expr);
                }

                if (queryExpression.IsNoTracking)
                {
                    (result as ObjectQuery <TEntity>).MergeOption = MergeOption.NoTracking;
                }


                if (queryExpression.OrderByExpression != null)
                {
                    result = OrderByData(result, queryExpression.OrderByExpression);
                }
                if (queryExpression.Pager != null)
                {
                    result = FilterPage(result, queryExpression.Pager);
                }

                return(result);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #2
0
File: Common.cs Project: jjg0519/OA
        public static Expression CovertToExpression(Type type, QueryExpression qExpression)
        {
            if (string.IsNullOrEmpty(qExpression.PropertyName))
            {
                return(null);
            }
            ParameterExpression param     = Expression.Parameter(type, "c");
            Expression          condition = null;
            // Merege Expression
            List <QueryExpression> list = new List <QueryExpression>();

            list.Add(qExpression);
            QueryExpression rE = qExpression.RelatedExpression;

            while (rE != null)
            {
                list.Add(rE);
                rE = rE.RelatedExpression;
            }

            for (int i = list.Count - 1; i >= 0; i--)
            {
                QueryExpression currentExpression = list[i];
                if (string.IsNullOrEmpty(currentExpression.PropertyName))
                {
                    continue;
                }


                Expression left         = GetLeft(param, currentExpression.PropertyName);
                Type       propertyType = left.Type;

                Expression right = null;
                if (list[i].RightPropertyValue != null)
                {
                    right = Expression.Constant(list[i].RightPropertyValue);
                }
                else
                {
                    object cValue = TryConvertValue(propertyType, currentExpression.PropertyValue);
                    right = Expression.Constant(cValue, propertyType);
                }

                Expression filter = QueryExpressionHelper.MergeExpression(currentExpression.Operation, left, right);

                if (condition == null)
                {
                    condition = filter;
                }
                else if (currentExpression.RelatedType == QueryExpression.RelationType.And)
                {
                    condition = Expression.And(condition, filter);
                }
                else
                {
                    condition = Expression.Or(condition, filter);
                }
            }

            if (condition == QueryExpressionHelper.EmptyExpression)
            {
                return(null);
            }

            return(Expression.Lambda(condition, param));
        }