IQueryable IODataServiceContext.Query(string contentTypeDefinition) { this._contentQuery = this._contentManager .Query() .ForType(new string[] { contentTypeDefinition }) .ForVersion(VersionOptions.Published); if (this._includes.Any()) { var contentItem = this._contentManager.New(contentTypeDefinition); var contentPartTypes = contentItem .Parts .Where(part => this._includes.Contains(part.PartDefinition.Name)) .Select(part => part.GetType()); var expandPartsMethod = typeof(QueryHints) .GetMethods() .FirstOrDefault(m => m.IsGenericMethod && m.GetGenericArguments().Count() == contentPartTypes.Count() && m.Name == "ExpandParts"); if (null != expandPartsMethod) { expandPartsMethod = expandPartsMethod.MakeGenericMethod(contentPartTypes.ToArray()); var queryHints = (QueryHints)expandPartsMethod.Invoke(new QueryHints(), new object[] { }); this._contentQuery.WithQueryHints(queryHints); } } var memberExpression = MemberExpression.Property(MemberExpression.Constant(this), typeof(ODataServiceContext), "Query"); return((this as IQueryProvider).CreateQuery <ContentItem>(memberExpression)); }
private static Expression GenerateRuleDecisionExpression(List <RuleDecisionElement> decisions, ref ParameterExpression parameterExpression) { List <Expression> blockExpressions = new List <Expression>(); foreach (var decision in decisions) { var memberExpression = MemberExpression.Property(parameterExpression, decision.PropertyName); var constantExpression = MemberExpression.Constant(decision.Value, typeof(string)); blockExpressions.Add(Expression.Assign(memberExpression, constantExpression)); } return(Expression.Block(blockExpressions)); }
private static Expression <Func <TContext, bool> > GenerateRuleExpression(RuleElement rule) { try { var parameterExpression = Expression.Parameter(typeof(TContext), "obj"); List <Expression> blockExpressions = new List <Expression>(); // Build Evaluation rules Expression finalBinaryExpression = Expression.Constant(true); var evaluations = rule.RuleEvaluations.Cast <RuleEvaluationElement>().ToList(); var decisions = rule.RuleDecisions.Cast <RuleDecisionElement>().ToList(); evaluations .ForEach(ruleEvaluation => { var memberExpression = MemberExpression.Property(parameterExpression, ruleEvaluation.PropertyName); var constantExpression = MemberExpression.Constant(ruleEvaluation.Value, typeof(string)); ExpressionType binaryExpressionType; if (ExpressionType.TryParse(ruleEvaluation.Criteria, true, out binaryExpressionType)) { var binaryExpression = Expression.MakeBinary(binaryExpressionType, memberExpression, constantExpression); finalBinaryExpression = Expression.And(finalBinaryExpression, binaryExpression); } }); var conditionalExpression = Expression.IfThen(finalBinaryExpression, GenerateRuleDecisionExpression(decisions, ref parameterExpression)); blockExpressions.Add(conditionalExpression); LabelTarget returnTarget = Expression.Label(typeof(bool)); blockExpressions.Add(Expression.Return(returnTarget, Expression.Constant(true), typeof(bool))); blockExpressions.Add(Expression.Label(returnTarget, Expression.Constant(true))); BlockExpression lambdaBodyExpression = Expression.Block(blockExpressions); // Compile and return var lambdaExpression = Expression.Lambda <Func <TContext, bool> >(lambdaBodyExpression, parameterExpression); return(lambdaExpression); } catch (Exception) { throw; } }