public static SparqlQueryContext GenerateSparqlQuery(EntityContext context, QueryModel queryModel)
 {
     var visitor = new SparqlGeneratorQueryModelVisitor(context);
     visitor.VisitQueryModel(queryModel);
     var resultType = queryModel.GetResultType();
     if (resultType.IsGenericType)
     {
         resultType = resultType.GetGenericArguments()[0];
     }
     var bindType = context.GetImplType(resultType);
     bool useDescribe = typeof (IEntityObject).IsAssignableFrom(bindType);
     return visitor.GetSparqlQuery(useDescribe);
 }
            /// <summary>
            /// The main entry point that we watch - we generate a method info stub when we need it
            /// from this.
            /// </summary>
            /// <param name="queryModel"></param>
            public override void VisitQueryModel(QueryModel queryModel)
            {
                // If the type is something that is friendly to be returned from a
                // method, then we should cache this guy.

                _qmContextStack.Push(new QMContext());

                // Now, run through the query model.

                base.VisitQueryModel(queryModel);

                // And if the QM result type is something we can reasonably cache, then we should do it.
                //  - Do not cache the outter most QM. This guy has the best place to start combining things.
                //  - Do not cache anything that is enumerable. We'll have to deal with that later.
                //  - Do not cache any anonymous types
                //  - Deal with later somethign that is an iterator (used in a later loop).

                var isEnumerable = typeof(IEnumerable).IsAssignableFrom(queryModel.GetResultType());
                var selectSequence = !queryModel.ResultOperators.Any();
                if (_qmContextStack.Count > 1
                    && ((selectSequence && isGoodSelectSequenceType(queryModel.GetResultType())) || !isEnumerable)
                    && !queryModel.GetResultType().IsClass
                    )
                {
                    var qmText = FormattingQueryVisitor.Format(queryModel);
                    if (!FoundFunctions.Where(ff => ff.QMText == qmText).Any())
                    {
                        var sref = _qmContextStack.Peek();
                        var f = new QMFuncHeader()
                        {
                            QM = queryModel,
                            QMText = qmText,
                            Arguments = sref._arguments.Cast<object>(),
                            IsSequence = selectSequence
                        };
                        FoundFunctions.Add(f);
                    }
                }

                // Go back to working on the previous qm.
                _qmContextStack.Pop();
            }
		private static Expression ProcessSubquery(ISessionFactory sessionFactory, ICollection<ExpressionHolder> elementExpression, QueryModel queryModel, Expression @group, QueryModel subQueryModel)
		{
			var subQueryMainFromClause = subQueryModel.MainFromClause;

			var restrictions = subQueryModel.BodyClauses
											.OfType<WhereClause>()
											.Select(w => new NhWithClause(w.Predicate));

			var join = new NhJoinClause(subQueryMainFromClause.ItemName,
										subQueryMainFromClause.ItemType,
										subQueryMainFromClause.FromExpression,
										restrictions);

			queryModel.BodyClauses.Add(@join);

			var visitor = new SwapQuerySourceVisitor(subQueryMainFromClause, @join);

			queryModel.TransformExpressions(visitor.Swap);

			var selector = subQueryModel.SelectClause.Selector;

			var collectionType = subQueryModel.GetResultType();
			
			var elementType = selector.Type;

			var source = new QuerySourceReferenceExpression(@join);

			return BuildSubCollectionQuery(sessionFactory, elementExpression, @group, source, selector, elementType, collectionType);
		}