private ILoader GetLoader(ISession session)
        {
            using (new SessionIdLoggingContext(Session.SessionId))
            {
                if (indexProjection != null)
                {
                    ProjectionLoader loader = new ProjectionLoader();
                    loader.Init(session, SearchFactory, resultTransformer, indexProjection);
                    return(loader);
                }

                if (criteria != null)
                {
                    if (classes.GetLength(0) > 1)
                    {
                        throw new SearchException("Cannot mix criteria and multiple entity types");
                    }

                    if (criteria is CriteriaImpl)
                    {
                        string targetEntity = ((CriteriaImpl)criteria).EntityOrClassName;
                        if (classes.GetLength(0) == 1 && classes[0].FullName != targetEntity)
                        {
                            throw new SearchException("Criteria query entity should match query entity");
                        }

                        try
                        {
                            System.Type entityType = ((CriteriaImpl)criteria).GetRootEntityTypeIfAvailable();
                            classes = new System.Type[] { entityType };
                        }
                        catch (Exception e)
                        {
                            throw new SearchException("Unable to load entity class from criteria: " + targetEntity, e);
                        }
                    }

                    QueryLoader loader = new QueryLoader();
                    loader.Init(session, searchFactoryImplementor);
                    loader.EntityType = classes[0];
                    loader.Criteria   = criteria;
                    return(loader);
                }

                if (classes.GetLength(0) == 1)
                {
                    QueryLoader loader = new QueryLoader();
                    loader.Init(session, searchFactoryImplementor);
                    loader.EntityType = classes[0];
                    return(loader);
                }
                else
                {
                    ObjectLoader loader = new ObjectLoader();
                    loader.Init(session, searchFactoryImplementor);
                    return(loader);
                }
            }
        }