private IEnumerable <ActivityStreamRecord> RunQuery(IHqlQuery[] contentQueries, int pageId, int pageSize, int?userId, int?minimumActivityStreamId)
        {
            List <string> queries = new List <string>();

            foreach (var contentQuery in contentQueries)
            {
                DefaultHqlQuery defaultQuery = contentQuery as DefaultHqlQuery;

                // This is for filling _form proprty of the DefaultHqlQuery, otherwise ToHql will not work
                var aliasFactory = new DefaultAliasFactory(defaultQuery);
                aliasFactory.ContentItem();
                queries.Add(defaultQuery.ToHql(true));
            }

            queries = queries.Select(c => string.Format(CultureInfo.InvariantCulture, "c.RelatedVersion.Id in ({0})", c)).ToList();

            List <string> finalWhereConditions = new List <string> {
                string.Format(CultureInfo.InvariantCulture, "({0})", string.Join(" or ", queries))
            };

            if (userId.HasValue)
            {
                finalWhereConditions.Add(string.Format(CultureInfo.InvariantCulture, "c.User.Id <> {0}", userId.Value.ToString(CultureInfo.InvariantCulture)));
            }

            if (minimumActivityStreamId.HasValue)
            {
                finalWhereConditions.Add(string.Format(CultureInfo.InvariantCulture, "c.Id > {0}", minimumActivityStreamId.Value.ToString(CultureInfo.InvariantCulture)));
            }

            string condition = string.Join(" and ", finalWhereConditions);
            var    mainHql   = string.Format("select c from Orchard.CRM.Core.Models.ActivityStreamRecord as c WHERE {0} order by c.Id desc", condition);

            var session = this.transactionManager.GetSession();
            var query   = session
                          .CreateQuery(mainHql)
                          .SetResultTransformer(Transformers.AliasToEntityMap)
                          .SetCacheable(false)
                          .SetFirstResult(pageId * pageSize)
                          .SetMaxResults(pageSize).List <IDictionary>();

            return(query.Select(c => (ActivityStreamRecord)c["0"]).ToList());
        }
        private int Count(IHqlQuery[] contentQueries, int?userId, int minimumActivityStreamId)
        {
            List <string> queries = new List <string>();

            foreach (var contentQuery in contentQueries)
            {
                DefaultHqlQuery defaultQuery = contentQuery as DefaultHqlQuery;

                // This is for filling _form proprty of the DefaultHqlQuery, otherwise ToHql will not work
                var aliasFactory = new DefaultAliasFactory(defaultQuery);
                aliasFactory.ContentItem();
                queries.Add(defaultQuery.ToHql(true));
            }

            queries = queries.Select(c => string.Format(CultureInfo.InvariantCulture, "c.RelatedVersion.Id in ({0})", c)).ToList();

            List <string> finalWhereConditions = new List <string> {
                string.Format(CultureInfo.InvariantCulture, "({0})", string.Join(" or ", queries))
            };

            if (userId.HasValue)
            {
                finalWhereConditions.Add(string.Format(CultureInfo.InvariantCulture, "c.User.Id <> {0}", userId.Value.ToString(CultureInfo.InvariantCulture)));
            }

            if (minimumActivityStreamId > 0)
            {
                finalWhereConditions.Add(string.Format(CultureInfo.InvariantCulture, "c.Id > {0}", minimumActivityStreamId.ToString(CultureInfo.InvariantCulture)));;
            }

            string condition = string.Join(" and ", finalWhereConditions);
            var    mainHql   = string.Format("select COUNT(c) from Orchard.CRM.Core.Models.ActivityStreamRecord as c WHERE {0}", condition);

            var session = this.transactionManager.GetSession();

            return(Convert.ToInt32(session.CreateQuery(mainHql)
                                   .SetCacheable(true)
                                   .UniqueResult()));
        }