public virtual void VisitMainFromClause (MainFromClause fromClause, QueryModel queryModel)
    {
      ArgumentUtility.CheckNotNull ("fromClause", fromClause);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      // nothing to do here      
    }
    /// <summary>
    /// Clones this clause, registering its clone with the <paramref name="cloneContext"/>.
    /// </summary>
    /// <param name="cloneContext">The clones of all query source clauses are registered with this <see cref="CloneContext"/>.</param>
    /// <returns>A clone of this clause.</returns>
    public MainFromClause Clone (CloneContext cloneContext)
    {
      ArgumentUtility.CheckNotNull ("cloneContext", cloneContext);

      var clone = new MainFromClause (ItemName, ItemType, FromExpression);
      cloneContext.QuerySourceMapping.AddMapping (this, new QuerySourceReferenceExpression (clone));
      return clone;
    }
        /// <summary>
        /// Clones this clause, registering its clone with the <paramref name="cloneContext"/>.
        /// </summary>
        /// <param name="cloneContext">The clones of all query source clauses are registered with this <see cref="CloneContext"/>.</param>
        /// <returns>A clone of this clause.</returns>
        public MainFromClause Clone(CloneContext cloneContext)
        {
            ArgumentUtility.CheckNotNull("cloneContext", cloneContext);

            var clone = new MainFromClause(ItemName, ItemType, FromExpression);

            cloneContext.QuerySourceMapping.AddMapping(this, new QuerySourceReferenceExpression(clone));
            return(clone);
        }
    private MainFromClause CreateMainFromClause (ClauseGenerationContext clauseGenerationContext)
    {
      var fromClause = new MainFromClause (
          AssociatedIdentifier,
          QuerySourceElementType,
          ParsedExpression);

      clauseGenerationContext.AddContextInfo (this, fromClause);
      return fromClause;
    }
    public override void VisitMainFromClause (MainFromClause fromClause, QueryModel queryModel)
    {
      ArgumentUtility.CheckNotNull ("fromClause", fromClause);
      ArgumentUtility.CheckNotNull ("queryModel", queryModel);

      var subQueryExpression = fromClause.FromExpression as SubQueryExpression;
      if (subQueryExpression != null)
        FlattenSubQuery (subQueryExpression, fromClause, queryModel, 0);

      base.VisitMainFromClause (fromClause, queryModel);
    }
    /// <summary>
    /// Initializes a new instance of <see cref="QueryModel"/>
    /// </summary>
    /// <param name="mainFromClause">The <see cref="Clauses.MainFromClause"/> of the query. This is the starting point of the query, generating items 
    /// that are filtered and projected by the query.</param>
    /// <param name="selectClause">The <see cref="SelectClause"/> of the query. This is the end point of
    /// the query, it defines what is actually returned for each of the items coming from the <see cref="MainFromClause"/> and passing the 
    /// <see cref="BodyClauses"/>. After it, only the <see cref="ResultOperators"/> modify the result of the query.</param>
    public QueryModel (MainFromClause mainFromClause, SelectClause selectClause)
    {
      ArgumentUtility.CheckNotNull ("mainFromClause", mainFromClause);
      ArgumentUtility.CheckNotNull ("selectClause", selectClause);

      _uniqueIdentifierGenerator = new UniqueIdentifierGenerator();

      MainFromClause = mainFromClause;
      SelectClause = selectClause;

      BodyClauses = new ObservableCollection<IBodyClause>();
      BodyClauses.CollectionChanged += BodyClauses_CollectionChanged;

      ResultOperators = new ObservableCollection<ResultOperatorBase>();
      ResultOperators.CollectionChanged += ResultOperators_CollectionChanged;
    }
        public override void VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
        {
            if (fromClause.FromExpression as SubQueryExpression != null)
                throw new Exception("MainFromClause.FromExpression cant be SubQueryExpression because group by is handle in itself clause");

            string fromName = LinqUtility.ResolveCollectionName(this.Db, fromClause.ItemType);

            if (fromClause.FromExpression.Type.Name == "IGrouping`2")
            {
                var groupByClause = LinqUtility.PriorGroupBy(ParnetModelVisitor);

                var parentMVisitor = LinqUtility.FindParentModelVisitor(this);
                //parentMVisitor.GroupByNameCounter++;

                fromName = groupByClause[0].TranslateIntoName();

                fromName = LinqUtility.ResolvePropertyName(fromName);
            }

            if (fromClause.FromExpression.NodeType == ExpressionType.Constant
                || fromClause.FromExpression.Type.Name == "IGrouping`2")
            {
                if (fromClause.FromExpression.Type.Name == "AqlQueryable`1" || fromClause.FromExpression.Type.Name == "IGrouping`2")
                    QueryText.AppendFormat(" for {0} in {1} ", LinqUtility.ResolvePropertyName(fromClause.ItemName), fromName);
                else
                {
                    QueryText.AppendFormat(" for {0} in ", LinqUtility.ResolvePropertyName(fromClause.ItemName));
                    GetAqlExpression(fromClause.FromExpression, queryModel);
                }
            }
            else
            {
                QueryText.AppendFormat(" for {0} in ", LinqUtility.ResolvePropertyName(fromClause.ItemName));
                GetAqlExpression(fromClause.FromExpression, queryModel);
            }

            base.VisitMainFromClause(fromClause, queryModel);
        }
    /// <summary>
    /// Creates a new <see cref="QueryModel"/> that has this <see cref="QueryModel"/> as a sub-query in its <see cref="MainFromClause"/>.
    /// </summary>
    /// <param name="itemName">The name of the new <see cref="QueryModel"/>'s <see cref="FromClauseBase.ItemName"/>.</param>
    /// <returns>A new <see cref="QueryModel"/> whose <see cref="MainFromClause"/>'s <see cref="FromClauseBase.FromExpression"/> is a 
    /// <see cref="SubQueryExpression"/> that holds this <see cref="QueryModel"/> instance.</returns>
    public QueryModel ConvertToSubQuery (string itemName)
    {
      ArgumentUtility.CheckNotNullOrEmpty ("itemName", itemName);

      var outputDataInfo = GetOutputDataInfo() as StreamedSequenceInfo;
      if (outputDataInfo == null)
      {
        var message = string.Format (
            "The query must return a sequence of items, but it selects a single object of type '{0}'.",
            GetOutputDataInfo ().DataType);
        throw new InvalidOperationException (message);
      }

      // from x in (sourceItemQuery)
      // select x

      var mainFromClause = new MainFromClause (
          itemName, 
          outputDataInfo.ResultItemType, 
          new SubQueryExpression (this));
      var selectClause = new SelectClause (new QuerySourceReferenceExpression (mainFromClause));
      return new QueryModel (mainFromClause, selectClause);
    }