Esempio n. 1
0
        public IEnumerable <T> Get(string fieldName, object fieldValue, int baseIndex, short pageSize, out int count, string sortFieldName, SortDirection sortDirection)
        {
            IEnumerable <T> results = null;

            NHibernate.Criterion.DetachedCriteria crit = NHibernate.Criterion.DetachedCriteria.For <T>();

            //if pageSize>0 then do paging, otherwise no paging and all records will be displayed
            if (pageSize > 0)
            {
                crit.SetFirstResult(baseIndex).SetMaxResults(pageSize);
            }

            if (fieldValue == null || fieldValue == DBNull.Value)
            {
                crit.Add(Expression.IsNull(fieldName));
            }
            else
            {
                crit.Add(Expression.Eq(fieldName, fieldValue));
            }

            if (!(string.IsNullOrEmpty(sortFieldName) || string.IsNullOrWhiteSpace(sortFieldName)))
            {
                crit.AddOrder(new Order(sortFieldName, sortDirection == SortDirection.Ascending ? true : false));
            }

            results = crit.GetExecutableCriteria(this._session).List <T>();

            var rowCount = crit.SetFirstResult(0).GetExecutableCriteria(this._session).SetProjection(Projections.RowCount()).FutureValue <Int32>();

            count = rowCount.Value;

            return(results);
        }
Esempio n. 2
0
        public IEnumerable <T> Get(string fieldName, object fieldValue, int pageIndex, int pageSize, out int count, string sortFieldName, SortDirection sortDirection)
        {
            /*
             * var rowCount = _session.CreateCriteria(typeof(T)).SetProjection(Projections.RowCount()).FutureValue<Int32>();
             * IEnumerable<T> results = null;
             *
             * if (fieldValue == null || fieldValue == DBNull.Value)
             * {
             *  results = _session.CreateCriteria(typeof(T))
             *       .Add(Expression.IsNull(fieldName))
             *       .SetFirstResult(pageIndex * pageSize)
             *       .SetMaxResults(pageSize)
             *       .AddOrder(new Order(sortFieldName, sortDirection == SortDirection.Ascending ? true : false))
             *       .List<T>();
             * }
             * else
             * {
             *  results = _session.CreateCriteria(typeof(T))
             *      .Add(Expression.Eq(fieldName, fieldValue))
             *      .SetFirstResult(pageIndex * pageSize)
             *      .SetMaxResults(pageSize)
             *      .AddOrder(new Order(sortFieldName, sortDirection == SortDirection.Ascending ? true : false))
             *      .List<T>();
             * }
             *
             */

            //Rizwan:27-Jan-2012    Commented the above old code and re-wrote the code as rowCount was giving count of all rows in the table instead of those which satisfy in the criteria
            IEnumerable <T> results = null;

            NHibernate.Criterion.DetachedCriteria crit = NHibernate.Criterion.DetachedCriteria.For <T>();

            //if pageSize>0 then do paging, otherwise no paging and all records will be displayed
            if (pageSize > 0)
            {
                crit.SetFirstResult((int)pageIndex * pageSize).SetMaxResults(pageSize);
            }

            if (fieldValue == null || fieldValue == DBNull.Value)
            {
                crit.Add(Expression.IsNull(fieldName));
            }
            else
            {
                crit.Add(Expression.Eq(fieldName, fieldValue));
            }

            if (!(string.IsNullOrEmpty(sortFieldName) || string.IsNullOrWhiteSpace(sortFieldName)))
            {
                crit.AddOrder(new Order(sortFieldName, sortDirection == SortDirection.Ascending ? true : false));
            }

            results = crit.GetExecutableCriteria(this._session).List <T>();

            var rowCount = crit.SetFirstResult(0).GetExecutableCriteria(this._session).SetProjection(Projections.RowCount()).FutureValue <Int32>();

            count = rowCount.Value;

            return(results);
        }
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="detachedCriteria"></param>
 /// <returns></returns>
 public T UniqueResult <T>(NHibernate.Criterion.DetachedCriteria detachedCriteria)
     where T : class, new()
 {
     if (!this.IsOpen)
     {
         throw new InvalidOperationException("Repository must be open before retrive data.");
     }
     if (detachedCriteria == null)
     {
         throw new ArgumentNullException("detachedCriteria");
     }
     return(detachedCriteria.GetExecutableCriteria(_session).UniqueResult <T>());
 }
Esempio n. 4
0
        public void WhereWithCountOnCollection_Criteria2()
        {
            using (ISession session = CreateSession())
            {
                ICriteria c = session.CreateCriteria(typeof(EntityContainer), "RootClass");

                NEX.DetachedCriteria d = NEX.DetachedCriteria.For(typeof(EntityContainer))
                                         .CreateAlias("Tests", "Tests")
                                         .SetProjection(NEX.Projections.ProjectionList()
                                                        .Add(NEX.Projections.RowCount()))
                                         .Add(NEX.Property.ForName("Id").EqProperty("RootClass.Id"));

                c.Add(NEX.Subqueries.Lt(1, d));
                IList <EntityContainer> result = c.List <EntityContainer>();
                Assert.AreEqual(result.Count, 1);
                Assert.AreEqual(result[0].PStr, "Alkampfer");
                ICollection <String> k = session.SessionFactory.Dialect.Functions.Keys;
            }
        }
Esempio n. 5
0
        public void WhereWithCountOnCollection_Criteria()
        {
            using (ISession session = CreateSession())
            {
                ICriteria c = session.CreateCriteria(typeof(EntityContainer), "RootClass");

                NEX.DetachedCriteria d = NEX.DetachedCriteria.For(typeof(EntityTest))
                                         .SetProjection(NEX.Projections.RowCount())
                                         .Add(NEX.Property.ForName("Container").EqProperty("RootClass.Id"));

                //You can use also
                //.Add(NEX.Expression.EqProperty("Encontained", "RootClass.Id"));

                c.Add(NEX.Subqueries.Lt(1, d));
                IList <EntityContainer> result = c.List <EntityContainer>();
                Assert.AreEqual(result.Count, 1);
                Assert.AreEqual(result[0].PStr, "Alkampfer");
            }
        }
Esempio n. 6
0
 protected SubqueryExpression(String op, String quantifier, DetachedCriteria dc)
     : this(op, quantifier, dc, true)
 {
 }
 internal PropertySubqueryExpression(String propertyName, String op, String quantifier, DetachedCriteria dc)
     : base(op, quantifier, dc)
 {
     this.propertyName = propertyName;
 }
 protected SubqueryExpression(String op, String quantifier, DetachedCriteria dc)
 {
     criteriaImpl    = dc.GetCriteriaImpl();
     this.quantifier = quantifier;
     this.op         = op;
 }