Exemplo n.º 1
0
    public SubQueryExpression (QueryModel queryModel)
    {
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      QueryModel = queryModel;
      _type = queryModel.GetOutputDataInfo().DataType;
    }
Exemplo n.º 2
0
 private OrderByClause GetOrderByClause (QueryModel queryModel)
 {
   if (queryModel.BodyClauses.Count == 0)
     return null;
   else
     return queryModel.BodyClauses[queryModel.BodyClauses.Count - 1] as OrderByClause;
 }
		public override void VisitOrderByClause(OrderByClause orderByClause, QueryModel queryModel, int index)
		{
			foreach (Ordering ordering in orderByClause.Orderings)
			{
				ordering.Expression = JoinReplacer(queryModel, ordering.Expression);
			}
		}
		private void FlattenSubQuery(SubQueryExpression subQueryExpression, MainFromClause fromClause,
		                             QueryModel queryModel)
		{
			// Create a new client-side select for the outer
			// TODO - don't like calling GetGenericArguments here...
			var clientSideSelect = new ClientSideSelect(new NonAggregatingGroupBySelectRewriter().Visit(queryModel.SelectClause.Selector, subQueryExpression.Type.GetGenericArguments()[0], queryModel.MainFromClause));

			// Replace the outer select clause...
			queryModel.SelectClause = subQueryExpression.QueryModel.SelectClause;

			MainFromClause innerMainFromClause = subQueryExpression.QueryModel.MainFromClause;

			CopyFromClauseData(innerMainFromClause, fromClause);

			foreach (var bodyClause in subQueryExpression.QueryModel.BodyClauses)
			{
				queryModel.BodyClauses.Add(bodyClause);
			}

			// Move the result operator up 
			if (queryModel.ResultOperators.Count != 0)
			{
				throw new NotImplementedException();
			}

			queryModel.ResultOperators.Add(new NonAggregatingGroupBy((GroupResultOperator) subQueryExpression.QueryModel.ResultOperators[0]));
			queryModel.ResultOperators.Add(clientSideSelect);
		}
Exemplo n.º 5
0
 public void SetUp ()
 {
   _mainFromClause = ExpressionHelper.CreateMainFromClause_Int();
   _selectClause = ExpressionHelper.CreateSelectClause();
   _queryModel = new QueryModel (_mainFromClause, _selectClause);
   _querySourceMapping = new QuerySourceMapping();
 }
Exemplo n.º 6
0
 /// <summary>
 /// 将一组key=value添加入QueryModel.Items
 /// </summary>
 /// <param name="model">QueryModel</param>
 /// <param name="key">当前项的HtmlName</param>
 /// <param name="val">当前项的值</param>
 public static void AddSearchItem(QueryModel model, string key, string val)
 {
     string field = "", prefix = "", orGroup = "", method = "";
     var keywords = key.Split(']', ')', '}');
     //将Html中的name分割为我们想要的几个部分
     foreach (var keyword in keywords)
     {
         if (Char.IsLetterOrDigit(keyword[0])) field = keyword;
         var last = keyword.Substring(1);
         if (keyword[0] == '(') prefix = last;
         if (keyword[0] == '[') method = last;
         if (keyword[0] == '{') orGroup = last;
     }
     if (string.IsNullOrEmpty(method)) return;
     if (!string.IsNullOrEmpty(field))
     {
         var item = new ConditionItem
                        {
                            Field = field,
                            Value = val.Trim(),
                            Prefix = prefix,
                            OrGroup = orGroup,
                            Method = (QueryMethod) Enum.Parse(typeof (QueryMethod), method)
                        };
         model.Items.Add(item);
     }
 }
        public void NothingShouldGoBang()
        {
            var factStore = new MemoryFactStore();
            var aggregateRebuilder = new AggregateRebuilder(factStore);
            var snapshot = new QueryModel<Student>(aggregateRebuilder);
            var eventBroker = Substitute.For<IDomainEventBroker>();

            Guid studentId;

            using (var unitOfWork = new UnitOfWork(factStore, eventBroker, new SystemClock()))
            {
                var repository = new Repository<Student>(snapshot, unitOfWork);

                var student = Student.Create("Fred", "Flintstone");
                studentId = student.Id;
                repository.Add(student);

                unitOfWork.Complete();
            }

            using (var unitOfWork = new UnitOfWork(factStore, eventBroker, new SystemClock()))
            {
                var repository = new Repository<Student>(snapshot, unitOfWork);
                var student = repository.GetById(studentId);

                student.FirstName.ShouldBe("Fred");
                student.LastName.ShouldBe("Flintstone");
            }
        }
Exemplo n.º 8
0
    /// <summary>
    /// Accepts the specified visitor by calling its <see cref="IQueryModelVisitor.VisitSelectClause"/> method.
    /// </summary>
    /// <param name="visitor">The visitor to accept.</param>
    /// <param name="queryModel">The query model in whose context this clause is visited.</param>
    public void Accept (IQueryModelVisitor visitor, QueryModel queryModel)
    {
      ArgumentUtility.CheckNotNull ("visitor", visitor);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      visitor.VisitSelectClause (this, queryModel);
    }
Exemplo n.º 9
0
		public static void ReWrite(QueryModel model)
		{
			// firstly, get the group join clauses
			var groupJoin = model.BodyClauses.Where(bc => bc is GroupJoinClause).Cast<GroupJoinClause>();

			if (groupJoin.Count() == 0)
			{
				// No group join here..
				return;
			}

			// Now walk the tree to decide which groupings are fully aggregated (and can hence be done in hql)
			var aggregateDetectorResults = IsAggregatingGroupJoin(model, groupJoin);

			if (aggregateDetectorResults.AggregatingClauses.Count > 0)
			{
				// Re-write the select expression
				model.SelectClause.TransformExpressions(s => GroupJoinSelectClauseRewriter.ReWrite(s, aggregateDetectorResults));

				// Remove the aggregating group joins
				foreach (GroupJoinClause aggregatingGroupJoin in aggregateDetectorResults.AggregatingClauses)
				{
					model.BodyClauses.Remove(aggregatingGroupJoin);
				}
			}
		}
Exemplo n.º 10
0
    public void VisitQueryModel ()
    {
      var mainFromClauseMock = _mockRepository.StrictMock<MainFromClause> ("x", typeof (Cook), ExpressionHelper.CreateExpression());
      var selectClauseMock = _mockRepository.StrictMock<SelectClause> (ExpressionHelper.CreateExpression());
      var queryModel = new QueryModel (mainFromClauseMock, selectClauseMock);

      using (_mockRepository.Ordered())
      {
        _visitorMock
            .Expect (mock => mock.VisitQueryModel (queryModel))
            .CallOriginalMethod (OriginalCallOptions.CreateExpectation);
        mainFromClauseMock.Expect (mock => mock.Accept (_visitorMock, queryModel));
        _visitorMock
            .Expect (mock => PrivateInvoke.InvokeNonPublicMethod (mock, "VisitBodyClauses", queryModel.BodyClauses, queryModel));
        selectClauseMock.Expect (mock => mock.Accept (_visitorMock, queryModel));
        _visitorMock
            .Expect (mock => PrivateInvoke.InvokeNonPublicMethod (mock, "VisitResultOperators", queryModel.ResultOperators, queryModel));
      }

      _mockRepository.ReplayAll();

      queryModel.Accept (_visitorMock);

      _mockRepository.VerifyAll();
    }
Exemplo n.º 11
0
		private void FlattenSubQuery(SubQueryExpression subQueryExpression, FromClauseBase fromClause,
		                             QueryModel queryModel)
		{
			// Move the result operator up 
			if (queryModel.ResultOperators.Count != 0)
			{
				throw new NotImplementedException();
			}

			var groupBy = (GroupResultOperator) subQueryExpression.QueryModel.ResultOperators[0];

			// Replace the outer select clause...
			queryModel.SelectClause.TransformExpressions(s => GroupBySelectClauseRewriter.ReWrite(s, groupBy, subQueryExpression.QueryModel));

			queryModel.SelectClause.TransformExpressions(
				s =>
				new SwapQuerySourceVisitor(queryModel.MainFromClause, subQueryExpression.QueryModel.MainFromClause).Swap
					(s));

			MainFromClause innerMainFromClause = subQueryExpression.QueryModel.MainFromClause;
			CopyFromClauseData(innerMainFromClause, fromClause);

			foreach (var bodyClause in subQueryExpression.QueryModel.BodyClauses)
			{
				queryModel.BodyClauses.Add(bodyClause);
			}

			queryModel.ResultOperators.Add(groupBy);
		}
    public void NamedEntity ()
    {
      // from c in Cooks select c [with name = "inner"]
      var subMainFromClause = new MainFromClause ("c", typeof(Cook), Expression.Constant (Cooks));
      var subSelectClause = new SelectClause (new NamedExpression ("inner", new QuerySourceReferenceExpression (subMainFromClause)));
      var subQuery = new QueryModel (subMainFromClause, subSelectClause);

      // from x in (subQuery) where x.ID == null select x
      var outerMainFromClause = new MainFromClause ("x", typeof(Cook), new SubQueryExpression (subQuery));
      var outerWhereClause = new WhereClause(Expression.Equal(Expression.MakeMemberAccess (new QuerySourceReferenceExpression (outerMainFromClause), typeof (Cook).GetProperty ("ID")), Expression.Constant (1)));
      var outerSelectClause = new SelectClause (new QuerySourceReferenceExpression (outerMainFromClause));
      var outerQuery = new QueryModel (outerMainFromClause, outerSelectClause);
      outerQuery.BodyClauses.Add (outerWhereClause);

      CheckQuery (
          outerQuery, 
          "SELECT [q0].[inner_ID] AS [ID],[q0].[inner_FirstName] AS [FirstName],[q0].[inner_Name] AS [Name],"
          + "[q0].[inner_IsStarredCook] AS [IsStarredCook],[q0].[inner_IsFullTimeCook] AS [IsFullTimeCook],"
          + "[q0].[inner_SubstitutedID] AS [SubstitutedID],[q0].[inner_KitchenID] AS [KitchenID],"
          + "[q0].[inner_KnifeID] AS [KnifeID],[q0].[inner_KnifeClassID] AS [KnifeClassID] " 
          + "FROM (SELECT [t1].[ID] AS [inner_ID],"
          + "[t1].[FirstName] AS [inner_FirstName],[t1].[Name] AS [inner_Name],[t1].[IsStarredCook] AS [inner_IsStarredCook],"
          + "[t1].[IsFullTimeCook] AS [inner_IsFullTimeCook],[t1].[SubstitutedID] AS [inner_SubstitutedID],"
          + "[t1].[KitchenID] AS [inner_KitchenID],[t1].[KnifeID] AS [inner_KnifeID],[t1].[KnifeClassID] AS [inner_KnifeClassID] "
          + "FROM [CookTable] AS [t1]) AS [q0] WHERE ([q0].[inner_ID] = @1)",
          new CommandParameter("@1", 1));
    }
    public void SetUp ()
    {
      _detailSource = ExpressionHelper.CreateQueryable<Kitchen>();
      _sectorSource = ExpressionHelper.CreateQueryable<Restaurant>();

      var query = from s1 in ExpressionHelper.CreateQueryable<Cook>()
                  from sd in
                      (from sector in _sectorSource
                       where sector.ID > 10
                       select sector.SubKitchen)
                  from s2 in s1.Assistants
                  where sd.Name == "Maths"
                  select new NonTransformedTuple<Cook, Kitchen> (s1, sd);
      _queryModel = ExpressionHelper.ParseQuery (query);

      _mainFromClause = _queryModel.MainFromClause;
      _additionalFromClause1 = (AdditionalFromClause) _queryModel.BodyClauses[0];
      _additionalFromClause2 = (AdditionalFromClause) _queryModel.BodyClauses[1];
      _whereClause = (WhereClause) _queryModel.BodyClauses[2];
      _selectClause = _queryModel.SelectClause;

      var subQueryExpressionA = (SubQueryExpression) _additionalFromClause1.FromExpression;
      _innerMainFromClauseA = subQueryExpressionA.QueryModel.MainFromClause;
      _innerWhereClauseA = (WhereClause) subQueryExpressionA.QueryModel.BodyClauses[0];

      _visitor = new SubQueryFromClauseFlattener();
    }
Exemplo n.º 14
0
    /// <summary>
    /// Accepts the specified visitor by calling its <see cref="IQueryModelVisitor.VisitAdditionalFromClause"/> method.
    /// </summary>
    /// <param name="visitor">The visitor to accept.</param>
    /// <param name="queryModel">The query model in whose context this clause is visited.</param>
    /// <param name="index">The index of this clause in the <paramref name="queryModel"/>'s <see cref="QueryModel.BodyClauses"/> collection.</param>
    public virtual void Accept (IQueryModelVisitor visitor, QueryModel queryModel, int index)
    {
      ArgumentUtility.CheckNotNull ("visitor", visitor);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      visitor.VisitAdditionalFromClause (this, queryModel, index);
    }
Exemplo n.º 15
0
    /// <summary>
    /// Accepts the specified visitor by calling its <see cref="IQueryModelVisitor.VisitMainFromClause"/> method.
    /// </summary>
    /// <param name="visitor">The visitor to accept.</param>
    /// <param name="queryModel">The query model in whose context this clause is visited.</param>
    public virtual void Accept (IQueryModelVisitor visitor, QueryModel queryModel)
    {
      ArgumentUtility.CheckNotNull ("visitor", visitor);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      visitor.VisitMainFromClause (this, queryModel);
    }
Exemplo n.º 16
0
    /// <summary>
    /// Accepts the specified visitor by calling its <see cref="IQueryModelVisitor.VisitWhereClause"/> method.
    /// </summary>
    /// <param name="visitor">The visitor to accept.</param>
    /// <param name="queryModel">The query model in whose context this clause is visited.</param>
    /// <param name="index">The index of this clause in the <paramref name="queryModel"/>'s <see cref="QueryModel.BodyClauses"/> collection.</param>
    public void Accept (IQueryModelVisitor visitor, QueryModel queryModel, int index)
    {
      ArgumentUtility.CheckNotNull ("visitor", visitor);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      visitor.VisitWhereClause (this, queryModel, index);
    }
Exemplo n.º 17
0
 public ActionResult Update([DataSourceRequest] DataSourceRequest request, QueryModel model)
 {
     try
     {
         PopulatePriority();
         PopulateSuppliers();
         PopulateStatus();
         PopulateUsers();
         PopulateCategory();
         if (model!= null && !(model.StatusId > 0))
         ModelState.AddModelError("StatusId","Status field required.");
         
         
         
         
         if (model != null && ModelState.IsValid)
         {
             var user = userService.GetUserProfile(User.Identity.Name);
             if (user == null)
             throw new InvalidCredentialException("User has no permission for this operation");
             model.Query = HttpUtility.HtmlDecode(model.Query);
             model.RaisedById = user.UserId;
             model.Supplier = new SupplierModel() { Id = user.SupplierId };
             service.UpdateQuery(QueryConverter.ConvertBack(model));
             var data = service.GetSupplierQueries(user.SupplierId).Select(QueryConverter.Convert);
             return Json(data.ToDataSourceResult(request));
         }
         
         return Json(ModelState.ToDataSourceResult());
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException(ex.ToString());
     }
 }
Exemplo n.º 18
0
    protected override void ApplyNodeSpecificSemantics (QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
    {
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      var clause = new WhereClause (GetResolvedPredicate (clauseGenerationContext));
      queryModel.BodyClauses.Add (clause);
    }
Exemplo n.º 19
0
    /// <summary>
    /// Accepts the specified visitor by calling its <see cref="IQueryModelVisitor.VisitOrdering"/> method.
    /// </summary>
    /// <param name="visitor">The visitor to accept.</param>
    /// <param name="queryModel">The query model in whose context this clause is visited.</param>
    /// <param name="orderByClause">The <see cref="OrderByClause"/> in whose context this item is visited.</param>
    /// <param name="index">The index of this item in the <paramref name="orderByClause"/>'s <see cref="OrderByClause.Orderings"/> collection.</param>
    public virtual void Accept (IQueryModelVisitor visitor, QueryModel queryModel, OrderByClause orderByClause, int index)
    {
      ArgumentUtility.CheckNotNull ("visitor", visitor);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);
      ArgumentUtility.CheckNotNull ("orderByClause", orderByClause);

      visitor.VisitOrdering (this, queryModel, orderByClause, index);
    }
        public static ResultOperatorRewriterResult Rewrite(QueryModel queryModel)
        {
            ResultOperatorRewriter rewriter = new ResultOperatorRewriter();

            rewriter.VisitQueryModel(queryModel);

            return new ResultOperatorRewriterResult(rewriter.resultOperators, rewriter.evaluationType);
        }
    protected override void ApplyNodeSpecificSemantics (QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
    {
      //NOTE: Do not seal ApplyNodeSpecificSemantics() in ResultOperatorExpressionNodeBase. It is overridden by e.g. Fetch-operators.
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      var resultOperator = CreateResultOperator (clauseGenerationContext);
      queryModel.ResultOperators.Add (resultOperator);
    }
    protected sealed override QueryModel WrapQueryModelAfterEndOfQuery (QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
    {
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      // Result operators can safely be appended to the previous query model even after another result operator, so do not wrap the previous
      // query model.
      return queryModel;
    }
Exemplo n.º 23
0
        public static IQuerySource FindQuerySource(QueryModel queryModel, System.Type type)
        {
            var finder = new QuerySourceLocator(type);

            finder.VisitQueryModel(queryModel);

            return finder._querySource;
        }
        protected override QueryModel ApplyNodeSpecificSemantics(QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
        {
            ArgumentUtility.CheckNotNull ("queryModel", queryModel);

              var resultOperator = CreateResultOperator (clauseGenerationContext);
              queryModel.ResultOperators.Add (resultOperator);
              return queryModel;
        }
Exemplo n.º 25
0
		private GroupJoinClause LocateGroupJoinQuerySource(QueryModel model)
		{
			if (model.BodyClauses.Count > 0)
			{
				return null;
			}
			return new LocateGroupJoinQuerySource(_results).Detect(model.MainFromClause.FromExpression);
		}
    public static FetchQueryModelBuilder[] RemoveFetchRequestsFromQueryModel (QueryModel queryModel)
    {
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      var visitor = new FetchFilteringQueryModelVisitor ();
      queryModel.Accept (visitor);
      return visitor.FetchQueryModelBuilders.ToArray();
    }
    protected override void ApplyNodeSpecificSemantics (QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
    {
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      var clause = new OrderByClause();
      clause.Orderings.Add (new Ordering (GetResolvedKeySelector (clauseGenerationContext), OrderingDirection.Desc));
      queryModel.BodyClauses.Add (clause);
    }
    public QueryModel Apply (QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
    {
      if (queryModel != null)
        throw new ArgumentException ("QueryModel has to be null because MainSourceExpressionNode marks the start of a query.", "queryModel");

      var mainFromClause = CreateMainFromClause (clauseGenerationContext);
      var defaultSelectClause = new SelectClause (new QuerySourceReferenceExpression (mainFromClause));
      return new QueryModel (mainFromClause, defaultSelectClause) { ResultTypeOverride = QuerySourceType };
    }
Exemplo n.º 29
0
    /// <summary>
    /// Initializes a new instance of the <see cref="FetchQueryModelBuilder"/> class.
    /// </summary>
    /// <param name="fetchRequest">The fetch request.</param>
    /// <param name="queryModel">The query model for which the <paramref name="fetchRequest"/> was originally defined.</param>
    /// <param name="resultOperatorPosition">The result operator position where the <paramref name="fetchRequest"/> was originally located.
    /// The <see cref="FetchQueryModelBuilder"/> will include all result operators prior to this position into the fetch <see cref="SourceItemQueryModel"/>,
    /// but it will not include any result operators occurring after (or at) that position.</param>
    public FetchQueryModelBuilder (FetchRequestBase fetchRequest, QueryModel queryModel, int resultOperatorPosition)
    {
      ArgumentUtility.CheckNotNull ("fetchRequest", fetchRequest);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      FetchRequest = fetchRequest;
      SourceItemQueryModel = queryModel;
      ResultOperatorPosition = resultOperatorPosition;
    }
    public QueryModel Apply (QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
    {
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      queryModel = WrapQueryModelAfterEndOfQuery (queryModel, clauseGenerationContext);
      ApplyNodeSpecificSemantics (queryModel, clauseGenerationContext);
      SetResultTypeOverride (queryModel);
      return queryModel;
    }
Exemplo n.º 31
0
        private static void FlattenSubQuery(SubQueryExpression subQueryExpression, FromClauseBase fromClause, QueryModel queryModel, int destinationIndex)
        {
            if (!CheckFlattenable(subQueryExpression.QueryModel))
            {
                return;
            }

            var mainFromClause = subQueryExpression.QueryModel.MainFromClause;

            CopyFromClauseData(mainFromClause, fromClause);

            var innerSelectorMapping = new QuerySourceMapping();

            innerSelectorMapping.AddMapping(fromClause, subQueryExpression.QueryModel.SelectClause.Selector);
            queryModel.TransformExpressions(ex => ReferenceReplacingExpressionVisitor.ReplaceClauseReferences(ex, innerSelectorMapping, false));

            InsertBodyClauses(subQueryExpression.QueryModel.BodyClauses, queryModel, destinationIndex);
            InsertResultOperators(subQueryExpression.QueryModel.ResultOperators, queryModel);

            var innerBodyClauseMapping = new QuerySourceMapping();

            innerBodyClauseMapping.AddMapping(mainFromClause, new QuerySourceReferenceExpression(fromClause));
            queryModel.TransformExpressions(ex => ReferenceReplacingExpressionVisitor.ReplaceClauseReferences(ex, innerBodyClauseMapping, false));
        }
Exemplo n.º 32
0
        public override Func <QueryContext, IAsyncEnumerable <TResult> > CompileAsyncQuery <TResult>(QueryModel queryModel)
        {
            Check.NotNull(queryModel, nameof(queryModel));

            var syncQueryExecutor = CompileQuery <TResult>(queryModel);

            return(qc => syncQueryExecutor(qc).ToAsyncEnumerable());
        }
Exemplo n.º 33
0
        public void VisitExpression_SubQuery()
        {
            QueryModel queryModel = ExpressionHelper.CreateQueryModel <Cook>();

            CheckDelegation("VisitSubQueryExpression", new SubQueryExpression(queryModel));
        }
Exemplo n.º 34
0
        public static void ReWrite(QueryModel model)
        {
            var rewriter = new MergeAggregatingResultsRewriter();

            rewriter.VisitQueryModel(model);
        }
Exemplo n.º 35
0
 public override void VisitWhereClause(WhereClause whereClause, QueryModel queryModel, int index)
 {
     whereClause.TransformExpressions(e => MergeAggregatingResultsInExpressionRewriter.Rewrite(e, new NameGenerator(queryModel)));
 }
Exemplo n.º 36
0
 public List <SampleDataModel> Get(QueryModel queryModel)
 {
     return(this.Get <SampleDataModel>(queryModel));
 }
        public static void ReWrite(QueryModel model)
        {
            var visitor = new QueryReferenceExpressionFlattener(model);

            model.TransformExpressions(visitor.Visit);
        }
Exemplo n.º 38
0
        private IEnumerable <IncludeSpecification> CreateIncludeSpecifications(
            QueryModel queryModel,
            IEnumerable <IncludeResultOperator> includeResultOperators)
        {
            var querySourceTracingExpressionVisitor
                = _querySourceTracingExpressionVisitorFactory.Create();

            return(includeResultOperators
                   .Select(includeResultOperator =>
            {
                var entityType
                    = _queryCompilationContext.Model
                      .FindEntityType(includeResultOperator.PathFromQuerySource.Type);

                var parts = includeResultOperator.NavigationPropertyPaths.ToArray();
                var navigationPath = new INavigation[parts.Length];

                for (var i = 0; i < parts.Length; i++)
                {
                    navigationPath[i] = entityType.FindNavigation(parts[i]);

                    if (navigationPath[i] == null)
                    {
                        throw new InvalidOperationException(
                            CoreStrings.IncludeBadNavigation(parts[i], entityType.DisplayName()));
                    }

                    entityType = navigationPath[i].GetTargetType();
                }

                var querySourceReferenceExpression
                    = querySourceTracingExpressionVisitor
                      .FindResultQuerySourceReferenceExpression(
                          queryModel.SelectClause.Selector,
                          includeResultOperator.QuerySource);

                if (querySourceReferenceExpression == null)
                {
                    _queryCompilationContext.Logger
                    .LogWarning(
                        CoreEventId.IncludeIgnoredWarning,
                        () => CoreStrings.LogIgnoredInclude(
                            $"{includeResultOperator.QuerySource.ItemName}.{navigationPath.Select(n => n.Name).Join(".")}"));
                }

                return new IncludeSpecification(
                    includeResultOperator,
                    querySourceReferenceExpression,
                    navigationPath);
            })
                   .Where(a =>
            {
                if (a.QuerySourceReferenceExpression == null)
                {
                    return false;
                }

                var sequenceType = a.QuerySourceReferenceExpression.Type.TryGetSequenceType();

                if (sequenceType != null &&
                    _queryCompilationContext.Model.FindEntityType(sequenceType) != null)
                {
                    return false;
                }

                return !a.NavigationPath.Any(n => n.IsCollection());
            })
                   .ToArray());
        }
Exemplo n.º 39
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual void CompileIncludes(
            [NotNull] QueryModel queryModel,
            [NotNull] ICollection <IncludeResultOperator> includeResultOperators,
            bool trackingQuery)
        {
            var includeGroupings
                = CreateIncludeSpecifications(queryModel, includeResultOperators)
                  .GroupBy(a => a.QuerySourceReferenceExpression);

            foreach (var includeGrouping in includeGroupings)
            {
                var entityParameter = Expression.Parameter(includeGrouping.Key.Type, name: "entity");

                var propertyExpressions = new List <Expression>();
                var blockExpressions    = new List <Expression>();

                if (trackingQuery)
                {
                    blockExpressions.Add(
                        Expression.Call(
                            _queryBufferParameter,
                            _startTrackingMethodInfo,
                            entityParameter,
                            Expression.Constant(
                                _queryCompilationContext.Model
                                .FindEntityType(entityParameter.Type))));
                }

                var includedIndex = 0;

                foreach (var includeSpecification in includeGrouping)
                {
                    _queryCompilationContext.Logger
                    .LogDebug(
                        CoreEventId.IncludingNavigation,
                        () => CoreStrings.LogIncludingNavigation(includeSpecification.IncludeResultOperator));

                    propertyExpressions.AddRange(
                        includeSpecification.NavigationPath
                        .Select((t, i) =>
                                includeSpecification.NavigationPath
                                .Take(i + 1)
                                .Aggregate(
                                    (Expression)includeSpecification.QuerySourceReferenceExpression,
                                    EntityQueryModelVisitor.CreatePropertyExpression)));

                    blockExpressions.Add(
                        BuildIncludeExpressions(
                            includeSpecification.NavigationPath,
                            entityParameter,
                            trackingQuery,
                            ref includedIndex,
                            navigationIndex: 0));

                    // TODO: Hack until new Include fully implemented
                    includeResultOperators.Remove(includeSpecification.IncludeResultOperator);
                }

                var includeReplacingExpressionVisitor
                    = new IncludeReplacingExpressionVisitor(
                          includeGrouping.Key,
                          Expression.Call(
                              IncludeMethodInfo.MakeGenericMethod(includeGrouping.Key.Type),
                              includeGrouping.Key,
                              Expression.NewArrayInit(
                                  typeof(object),
                                  propertyExpressions),
                              trackingQuery
                                ? (Expression)
                              Expression.Property(
                                  Expression.Property(
                                      EntityQueryModelVisitor.QueryContextParameter,
                                      propertyName: "StateManager"),
                                  propertyName: "Value")
                                : Expression.Default(typeof(IStateManager)),
                              trackingQuery
                                ? (Expression)
                              Expression.Property(
                                  EntityQueryModelVisitor.QueryContextParameter,
                                  propertyName: "QueryBuffer")
                                : Expression.Default(typeof(IQueryBuffer)),
                              Expression.Lambda(
                                  Expression.Block(typeof(void), blockExpressions),
                                  entityParameter,
                                  _includedParameter,
                                  _stateManagerParameter,
                                  _queryBufferParameter)));

                queryModel.SelectClause.TransformExpressions(includeReplacingExpressionVisitor.Visit);
            }
        }
Exemplo n.º 40
0
        /// <summary>
        /// Executes the sql query and returns the data results
        /// </summary>
        /// <typeparam name="T">Data type in the main from clause (queryModel.MainFromClause.ItemType)</typeparam>
        /// <param name="queryModel">Linq query model</param>
        protected IEnumerable <object> GetDataResults(SqlParts sql, QueryModel queryModel)
        {
            IEnumerable <object> results;
            OleDbDataReader      data = null;

            var conn    = ExcelUtilities.GetConnection(_args);
            var command = conn.CreateCommand();

            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                command.CommandText = sql.ToString();
                command.Parameters.AddRange(sql.Parameters.ToArray());
                try { data = command.ExecuteReader(); }
                catch (OleDbException e)
                {
                    if (e.Message.Contains(_args.WorksheetName))
                    {
                        throw new DataException(
                                  string.Format("'{0}' is not a valid worksheet name in file {3}. Valid worksheet names are: '{1}'. Error received: {2}",
                                                _args.WorksheetName, string.Join("', '", ExcelUtilities.GetWorksheetNames(_args.FileName).ToArray()), e.Message, _args.FileName), e);
                    }
                    if (!CheckIfInvalidColumnNameUsed(sql))
                    {
                        throw e;
                    }
                }

                var columns = ExcelUtilities.GetColumnNames(data);
                LogColumnMappingWarnings(columns);
                if (columns.Count() == 1 && columns.First() == "Expr1000")
                {
                    results = GetScalarResults(data);
                }
                else if (queryModel.MainFromClause.ItemType == typeof(Row))
                {
                    results = GetRowResults(data, columns);
                }
                else if (queryModel.MainFromClause.ItemType == typeof(RowNoHeader))
                {
                    results = GetRowNoHeaderResults(data);
                }
                else
                {
                    results = GetTypeResults(data, columns, queryModel);
                }
            }
            finally
            {
                command.Dispose();

                if (!_args.UsePersistentConnection)
                {
                    conn.Dispose();
                    _args.PersistentConnection = null;
                }
            }

            return(results);
        }
Exemplo n.º 41
0
        private IEnumerable <object> GetTypeResults(IDataReader data, IEnumerable <string> columns, QueryModel queryModel)
        {
            var results  = new List <object>();
            var fromType = queryModel.MainFromClause.ItemType;
            var props    = fromType.GetProperties();

            if (_args.StrictMapping.Value != StrictMappingType.None)
            {
                this.ConfirmStrictMapping(columns, props, _args.StrictMapping.Value);
            }

            while (data.Read())
            {
                var result = Activator.CreateInstance(fromType);
                foreach (var prop in props)
                {
                    var columnName = (_args.ColumnMappings.ContainsKey(prop.Name)) ?
                                     _args.ColumnMappings[prop.Name] :
                                     prop.Name;
                    if (columns.Contains(columnName))
                    {
                        var value = GetColumnValue(data, columnName, prop.Name).Cast(prop.PropertyType);
                        value = TrimStringValue(value);
                        result.SetProperty(prop.Name, value);
                    }
                }
                results.Add(result);
            }
            return(results.AsEnumerable());
        }
        private void AdjustForResultOperators(QueryModel queryModel)
        {
            var referencedQuerySource
                = queryModel.SelectClause.Selector.TryGetReferencedQuerySource()
                  ?? queryModel.MainFromClause.FromExpression.TryGetReferencedQuerySource();

            // If there is any CastResultOperator then we need to do client eval unless it is doing same cast.
            if (queryModel.ResultOperators.OfType <CastResultOperator>().Any() &&
                referencedQuerySource != null)
            {
                PromoteQuerySource(referencedQuerySource);

                return;
            }

            var isSubQuery          = _queryModelStack.Count > 0;
            var finalResultOperator = queryModel.ResultOperators.LastOrDefault();

            if (isSubQuery &&
                finalResultOperator is GroupResultOperator groupResultOperator &&
                queryModel.ResultOperators.OfType <GroupResultOperator>().Count() == 1)
            {
                if (!(groupResultOperator.KeySelector is MemberInitExpression))
                {
                    // This is to compensate for the fact that we have to demote querysources found in selector for GroupBy
                    // TODO: See #11215
                    foreach (var querySource in TraverseQuerySources(queryModel.SelectClause.Selector))
                    {
                        DemoteQuerySource(querySource);
                    }

                    if (groupResultOperator.ElementSelector is QuerySourceReferenceExpression qsre)
                    {
                        DemoteQuerySource(qsre.ReferencedQuerySource);
                    }

                    return;
                }
            }

            // The selector may not have been a QSRE but this query model may still have something that needs adjusted.
            // Example:
            // context.Orders.GroupBy(o => o.CustomerId).Select(g => new { g.Key, g.Sum(o => o.TotalAmount) })
            // The g.Sum(...) will result in a subquery model like { from Order o in [g] select o.TotalAmount => Sum() }.
            // In that case we need to ensure that the referenced query source [g] is demoted.
            if (referencedQuerySource == null)
            {
                return;
            }

            // If the GroupResultOperator is not last (as captured by above)
            // then we promote first GroupResultOperator to fall through streaming group by
            var firstGroupResultOperator = queryModel.ResultOperators.OfType <GroupResultOperator>().FirstOrDefault();

            if (firstGroupResultOperator != null)
            {
                PromoteQuerySource(firstGroupResultOperator);
            }

            var unreachableFromParentSelector =
                isSubQuery && _querySourceTracingExpressionVisitor
                .FindResultQuerySourceReferenceExpression(
                    _queryModelStack.Peek().SelectClause.Selector,
                    referencedQuerySource) == null;

            if (finalResultOperator is SingleResultOperator ||
                finalResultOperator is FirstResultOperator ||
                finalResultOperator is LastResultOperator)
            {
                // If not a subquery or if reachable from the parent selector
                // we would not want to fall through to one of the next blocks.
                if (unreachableFromParentSelector)
                {
                    DemoteQuerySourceAndUnderlyingFromClause(referencedQuerySource);
                }

                return;
            }

            if (ConvergesToSingleValue(queryModel)
                // This is to preserve QuerySource from MainFromClause when selector is ConstantExpression
                // Since we would cause client evaluation from ConstantExpression.
                // TODO: See #11215
                && !(queryModel.SelectClause.Selector is ConstantExpression))
            {
                // This is a top-level query that was not Single/First/Last
                // but returns a single/scalar value (Avg/Min/Max/etc.)
                // or a subquery that belongs to some outer-level query that returns
                // a single or scalar value. The referenced query source should be
                // re-promoted later if necessary.

                // For top-level Contains we cannot translate it since Item is not Expression
                if (!isSubQuery &&
                    finalResultOperator is ContainsResultOperator containsResultOperator)
                {
                    return;
                }

                // If we are selecting from Grouping source but it is not GroupByAggregate Query
                // then do not demote Grouping source since we still need to create groups.
                if (queryModel.MainFromClause.FromExpression.Type.IsGrouping() &&
                    !IsGroupByAggregateSubQuery(queryModel))
                {
                    return;
                }

                DemoteQuerySourceAndUnderlyingFromClause(referencedQuerySource);
                return;
            }

            if (isSubQuery && (unreachableFromParentSelector || finalResultOperator is DefaultIfEmptyResultOperator))
            {
                DemoteQuerySourceAndUnderlyingFromClause(referencedQuerySource);
            }
        }
Exemplo n.º 43
0
 /// <summary>
 /// Executes a query with a scalar result, i.e. a query that ends with a result operator such as Count, Sum, or Average.
 /// </summary>
 public T ExecuteScalar <T>(QueryModel queryModel)
 {
     return(ExecuteSingle <T>(queryModel, false));
 }
Exemplo n.º 44
0
        public virtual Func <QueryContext, IAsyncEnumerable <TResult> > CreateAsyncQueryExecutor <TResult>([NotNull] QueryModel queryModel)
        {
            Check.NotNull(queryModel, nameof(queryModel));

            using (QueryCompilationContext.Logger.BeginScopeImpl(this))
            {
                QueryCompilationContext.Logger
                .LogVerbose(
                    CoreLoggingEventId.CompilingQueryModel,
                    () => CoreStrings.LogCompilingQueryModel(queryModel));

                _blockTaskExpressions = false;

                ExtractQueryAnnotations(queryModel);

                OptimizeQueryModel(queryModel);

                QueryCompilationContext.FindQuerySourcesRequiringMaterialization(this, queryModel);
                QueryCompilationContext.DetermineQueryBufferRequirement(queryModel);

                VisitQueryModel(queryModel);

                AsyncSingleResultToSequence(queryModel);

                IncludeNavigations(queryModel);

                TrackEntitiesInResults <TResult>(queryModel);

                InterceptExceptions();

                return(CreateExecutorLambda <IAsyncEnumerable <TResult> >());
            }
        }
Exemplo n.º 45
0
 /// <summary>
 /// Modifies the given query model for fetching, adding new <see cref="AdditionalFromClause"/> instances and changing the
 /// <see cref="SelectClause"/> as needed.
 /// This method is called by <see cref="CreateFetchQueryModel"/> in the process of creating the new fetch query model.
 /// </summary>
 /// <param name="fetchQueryModel">The fetch query model to modify.</param>
 protected abstract void ModifyFetchQueryModel(QueryModel fetchQueryModel);
Exemplo n.º 46
0
 public async Task <PageData <T> > GetPageList(QueryModel queryModel)
 {
     return(await DapperExtensions.PageQuery <T>(_unitOfWork, queryModel));
 }
 private QueryReferenceExpressionFlattener(QueryModel model)
 {
     _model = model;
 }
Exemplo n.º 48
0
        public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
        {
            //Affects SQL result operators
            if (resultOperator is TakeResultOperator)
            {
                var take = resultOperator as TakeResultOperator;
                SqlStatement.Aggregate = string.Format("TOP {0} *", take.Count);
            }
            else if (resultOperator is AverageResultOperator)
            {
                UpdateAggregate(queryModel, "AVG");
            }
            else if (resultOperator is CountResultOperator)
            {
                SqlStatement.Aggregate = "COUNT(*)";
            }
            else if (resultOperator is LongCountResultOperator)
            {
                SqlStatement.Aggregate = "COUNT(*)";
            }
            else if (resultOperator is FirstResultOperator)
            {
                SqlStatement.Aggregate = "TOP 1 *";
            }
            else if (resultOperator is MaxResultOperator)
            {
                UpdateAggregate(queryModel, "MAX");
            }
            else if (resultOperator is MinResultOperator)
            {
                UpdateAggregate(queryModel, "MIN");
            }
            else if (resultOperator is SumResultOperator)
            {
                UpdateAggregate(queryModel, "SUM");
            }
            else if (resultOperator is DistinctResultOperator)
            {
                ProcessDistinctAggregate(queryModel);
            }

            //Not supported result operators
            else if (resultOperator is ContainsResultOperator)
            {
                throw new NotSupportedException("LinqToExcel does not provide support for the Contains() method");
            }
            else if (resultOperator is DefaultIfEmptyResultOperator)
            {
                throw new NotSupportedException("LinqToExcel does not provide support for the DefaultIfEmpty() method");
            }
            else if (resultOperator is ExceptResultOperator)
            {
                throw new NotSupportedException("LinqToExcel does not provide support for the Except() method");
            }
            else if (resultOperator is GroupResultOperator)
            {
                throw new NotSupportedException("LinqToExcel does not provide support for the Group() method");
            }
            else if (resultOperator is IntersectResultOperator)
            {
                throw new NotSupportedException("LinqToExcel does not provide support for the Intersect() method");
            }
            else if (resultOperator is OfTypeResultOperator)
            {
                throw new NotSupportedException("LinqToExcel does not provide support for the OfType() method");
            }
            else if (resultOperator is SingleResultOperator)
            {
                throw new NotSupportedException("LinqToExcel does not provide support for the Single() method. Use the First() method instead");
            }
            else if (resultOperator is UnionResultOperator)
            {
                throw new NotSupportedException("LinqToExcel does not provide support for the Union() method");
            }

            base.VisitResultOperator(resultOperator, queryModel, index);
        }
        public new Func <QueryContext, IAsyncEnumerable <TResult> > CreateQueryExecutor <TResult>([NotNull] QueryModel queryModel)
        {
            Check.NotNull(queryModel, "queryModel");

            VisitQueryModel(queryModel);

            if (_streamedSequenceInfo == null)
            {
                _expression
                    = Expression.Call(
                          _taskToSequenceShim.MakeGenericMethod(typeof(TResult)),
                          _expression);
            }

            return(Expression
                   .Lambda <Func <QueryContext, IAsyncEnumerable <TResult> > >(_expression, _queryContextParameter)
                   .Compile());
        }
Exemplo n.º 50
0
 public override void VisitJoinClause(JoinClause joinClause, QueryModel queryModel, int index)
 {
     throw new NotSupportedException("LinqToExcel does not provide support for the Join() method");
 }
Exemplo n.º 51
0
 public override void VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
 {
     selectClause.TransformExpressions(e => MergeAggregatingResultsInExpressionRewriter.Rewrite(e, new NameGenerator(queryModel)));
 }
Exemplo n.º 52
0
 public override void VisitGroupJoinClause(GroupJoinClause groupJoinClause, QueryModel queryModel, int index)
 {
     throw new NotSupportedException("LinqToExcel does not provide support for group join");
 }
Exemplo n.º 53
0
 public async Task <IViewComponentResult> InvokeAsync(string field, QueryModel queryModel)
 {
     //  daydate = DateTime.Now;
     ViewData["field"] = field;
     return(await Task.FromResult((IViewComponentResult)View("Default", queryModel)));
 }
Exemplo n.º 54
0
        protected override void VisitBodyClauses(ObservableCollection <IBodyClause> bodyClauses, QueryModel queryModel)
        {
            var orderClause = bodyClauses
                              .FirstOrDefault(x => x.GetType() == typeof(OrderByClause))
                              as OrderByClause;

            if (orderClause != null)
            {
                var columnName = "";
                var exp        = orderClause.Orderings.First().Expression;
                if (exp is MemberExpression)
                {
                    var mExp = exp as MemberExpression;
                    columnName = (_args.ColumnMappings.ContainsKey(mExp.Member.Name)) ?
                                 _args.ColumnMappings[mExp.Member.Name] :
                                 mExp.Member.Name;
                }
                else if (exp is MethodCallExpression)
                {
                    //row["ColumnName"] is being used in order by statement
                    columnName = ((MethodCallExpression)exp).Arguments.First()
                                 .ToString().Replace("\"", "");
                }

                SqlStatement.OrderBy = columnName;
                SqlStatement.ColumnNamesUsed.Add(columnName);
                var orderDirection = orderClause.Orderings.First().OrderingDirection;
                SqlStatement.OrderByAsc = (orderDirection == OrderingDirection.Asc);
            }
            base.VisitBodyClauses(bodyClauses, queryModel);
        }
        public virtual void Rewrite([NotNull] QueryModel queryModel)
        {
            Check.NotNull(queryModel, nameof(queryModel));

            queryModel.TransformExpressions(Visit);
        }
Exemplo n.º 56
0
 ICloneableQueryAnnotation ICloneableQueryAnnotation.Clone(IQuerySource querySource, QueryModel queryModel)
 => new ValueFromOpenJsonOperator(ParseInfo, Json, Path)
 {
     QuerySource = querySource, QueryModel = queryModel
 };
Exemplo n.º 57
0
        internal static void InsertResultOperators(IEnumerable <ResultOperatorBase> resultOperators, QueryModel queryModel)
        {
            var index = 0;

            foreach (var bodyClause in resultOperators)
            {
                queryModel.ResultOperators.Insert(index, bodyClause);
                ++index;
            }
        }
        private SubQueryExpression CreateSubQueryNode(MethodCallExpression methodCallExpression)
        {
            QueryModel queryModel = _queryParser.GetParsedQuery(methodCallExpression);

            return(new SubQueryExpression(queryModel));
        }
Exemplo n.º 59
0
        protected virtual void TrackEntitiesInResults <TResult>(
            [NotNull] QueryModel queryModel)
        {
            Check.NotNull(queryModel, nameof(queryModel));

            var lastTrackingModifier
                = QueryCompilationContext.QueryAnnotations
                  .OfType <QueryAnnotation>()
                  .LastOrDefault(
                      qa => qa.IsCallTo(EntityFrameworkQueryableExtensions.AsNoTrackingMethodInfo) ||
                      qa.IsCallTo(EntityFrameworkQueryableExtensions.AsTrackingMethodInfo));

            if (queryModel.GetOutputDataInfo() is StreamedScalarValueInfo ||
                lastTrackingModifier
                ?.IsCallTo(EntityFrameworkQueryableExtensions.AsNoTrackingMethodInfo) == true ||
                (!QueryCompilationContext.TrackQueryResults &&
                 lastTrackingModifier
                 ?.IsCallTo(EntityFrameworkQueryableExtensions.AsTrackingMethodInfo) != true))
            {
                return;
            }

            var entityTrackingInfos
                = _entityResultFindingExpressionVisitorFactory.Create(QueryCompilationContext)
                  .FindEntitiesInResult(queryModel.SelectClause.Selector);

            if (entityTrackingInfos.Any())
            {
                var resultItemType     = _expression.Type.GetSequenceType();
                var resultItemTypeInfo = resultItemType.GetTypeInfo();

                MethodInfo trackingMethod;

                if (resultItemTypeInfo.IsGenericType &&
                    (resultItemTypeInfo.GetGenericTypeDefinition() == typeof(IGrouping <,>) ||
                     resultItemTypeInfo.GetGenericTypeDefinition() == typeof(IAsyncGrouping <,>)))
                {
                    trackingMethod
                        = LinqOperatorProvider.TrackGroupedEntities
                          .MakeGenericMethod(
                              resultItemType.GenericTypeArguments[0],
                              resultItemType.GenericTypeArguments[1],
                              queryModel.SelectClause.Selector.Type);
                }
                else
                {
                    trackingMethod
                        = LinqOperatorProvider.TrackEntities
                          .MakeGenericMethod(
                              resultItemType,
                              queryModel.SelectClause.Selector.Type);
                }

                _expression
                    = Expression.Call(
                          trackingMethod,
                          _expression,
                          QueryContextParameter,
                          Expression.Constant(entityTrackingInfos),
                          Expression.Constant(
                              _getEntityAccessors
                              .MakeGenericMethod(queryModel.SelectClause.Selector.Type)
                              .Invoke(
                                  null,
                                  new object[]
                {
                    entityTrackingInfos,
                    queryModel.SelectClause.Selector
                })));
            }
        }
Exemplo n.º 60
0
            protected void Compile(
                QueryCompilationContext queryCompilationContext,
                QueryModel queryModel,
                bool trackingQuery,
                bool asyncQuery,
                ref int collectionIncludeId,
                QuerySourceReferenceExpression targetQuerySourceReferenceExpression)
            {
                var entityParameter
                    = Expression.Parameter(targetQuerySourceReferenceExpression.Type, name: "entity");

                var propertyExpressions = new List <Expression>();
                var blockExpressions    = new List <Expression>();

                var entityType
                    = queryCompilationContext.FindEntityType(targetQuerySourceReferenceExpression.ReferencedQuerySource)
                      ?? queryCompilationContext.Model.FindEntityType(entityParameter.Type);

                if (entityType.IsQueryType)
                {
                    trackingQuery = false;
                }

                if (trackingQuery)
                {
                    blockExpressions.Add(
                        Expression.Call(
                            Expression.Property(
                                EntityQueryModelVisitor.QueryContextParameter,
                                nameof(QueryContext.QueryBuffer)),
                            _queryBufferStartTrackingMethodInfo,
                            entityParameter,
                            Expression.Constant(entityType)));
                }

                var includedIndex = 0;

                // ReSharper disable once LoopCanBeConvertedToQuery
                foreach (var includeLoadTreeNode in Children)
                {
                    blockExpressions.Add(
                        includeLoadTreeNode.Compile(
                            queryCompilationContext,
                            targetQuerySourceReferenceExpression,
                            entityParameter,
                            propertyExpressions,
                            trackingQuery,
                            asyncQuery,
                            ref includedIndex,
                            ref collectionIncludeId));
                }

                if (blockExpressions.Count > 1 ||
                    blockExpressions.Count == 1 &&
                    !trackingQuery)
                {
                    AwaitTaskExpressions(asyncQuery, blockExpressions);

                    var includeExpression
                        = blockExpressions.Last().Type == typeof(Task)
                            ? new TaskBlockingExpressionVisitor()
                          .Visit(
                              Expression.Call(
                                  _includeAsyncMethodInfo
                                  .MakeGenericMethod(targetQuerySourceReferenceExpression.Type),
                                  EntityQueryModelVisitor.QueryContextParameter,
                                  targetQuerySourceReferenceExpression,
                                  Expression.NewArrayInit(typeof(object), propertyExpressions),
                                  Expression.Lambda(
                                      Expression.Block(blockExpressions),
                                      EntityQueryModelVisitor.QueryContextParameter,
                                      entityParameter,
                                      _includedParameter,
                                      CancellationTokenParameter),
                                  CancellationTokenParameter))
                            : Expression.Call(
                              _includeMethodInfo.MakeGenericMethod(targetQuerySourceReferenceExpression.Type),
                              EntityQueryModelVisitor.QueryContextParameter,
                              targetQuerySourceReferenceExpression,
                              Expression.NewArrayInit(typeof(object), propertyExpressions),
                              Expression.Lambda(
                                  Expression.Block(typeof(void), blockExpressions),
                                  EntityQueryModelVisitor.QueryContextParameter,
                                  entityParameter,
                                  _includedParameter));

                    ApplyIncludeExpressionsToQueryModel(
                        queryModel, targetQuerySourceReferenceExpression, includeExpression);
                }
            }