/// <summary>
        /// </summary>
        /// <param name="user"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList <PersonalMessage> GetLastMessageForEachUser(User user, int pageIndex, int pageSize)
        {
            /*
             *   select *
             * from Msgs_PersonalMessage t
             * where (select count(1) from Msgs_PersonalMessage where publisher_id=t.publisher_id and createTime>t.createTime)<=1
             */
            DetachedCriteria detache = DetachedCriteria.For <PersonalMessage>("b")
                                       .Add(Restrictions.Eq(ReceiverProperty, user))
                                       .Add(Restrictions.Not(Restrictions.Eq(StateProperty, PersonalMessageStatus.Receiver)))
                                       .Add(Restrictions.LtProperty("b.CreateTime", "a.CreateTime"))
                                       .Add(Restrictions.EqProperty("a.Publisher", "b.Publisher"))
                                       .SetProjection(Projections.SqlProjection("count(1)+1",
                                                                                new[] { "sd" },
                                                                                new IType[]
            {
                NHibernateUtil
                .Int32
            }));

            //var d = detache.GetExecutableCriteria(this.CurrentSession).List<object>();
            return(DetachedCriteria.For <PersonalMessage>("a")
                   .Add(Restrictions.Eq(ReceiverProperty, user))
                   .Add(Restrictions.Not(Restrictions.Eq(StateProperty, PersonalMessageStatus.Receiver)))
                   .Add(Subqueries.Ge(1, detache))
                   .AddOrder(Order.Desc(Projections.Property <PersonalMessage>(s => s.CreateTime)))
                   .SetMaxResults(pageSize)
                   .SetFirstResult(pageIndex * pageSize)
                   .GetExecutableCriteria(CurrentSession)
                   .List <PersonalMessage>());
        }
Beispiel #2
0
        public void ValueCriterion()
        {
            ICriteria expected =
                CreateTestCriteria(typeof(Person))
                .Add(Subqueries.Eq("Name", DetachedCriteriaName))
                .Add(Subqueries.Ge("Age", DetachedCriteriaAge))
                .Add(Subqueries.Gt("Age", DetachedCriteriaAge))
                .Add(Subqueries.In("Name", DetachedCriteriaName))
                .Add(Subqueries.Le("Age", DetachedCriteriaAge))
                .Add(Subqueries.Lt("Age", DetachedCriteriaAge))
                .Add(Subqueries.Ne("Name", DetachedCriteriaName))
                .Add(Subqueries.NotIn("Name", DetachedCriteriaName));

            var actual =
                CreateTestQueryOver <Person>()
                .And(Subqueries.WhereValue("Name").Eq(DetachedQueryOverName))
                .And(Subqueries.WhereValue("Age").Ge(DetachedQueryOverAge))
                .And(Subqueries.WhereValue("Age").Gt(DetachedQueryOverAge))
                .And(Subqueries.WhereValue("Name").In(DetachedQueryOverName))
                .And(Subqueries.WhereValue("Age").Le(DetachedQueryOverAge))
                .And(Subqueries.WhereValue("Age").Lt(DetachedQueryOverAge))
                .And(Subqueries.WhereValue("Name").Ne(DetachedQueryOverName))
                .And(Subqueries.WhereValue("Name").NotIn(DetachedQueryOverName));

            AssertCriteriaAreEqual(expected, actual);
        }
Beispiel #3
0
        public void SubqueriesExpressions()
        {
            DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
                                  .Add(Expression.Eq("Name", "Gavin King"));
            ICriterion c = Subqueries.Eq("Gavin King", dc);

            NHAssert.IsSerializable(c);
            c = Subqueries.EqAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Exists(dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Ge("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GeAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GeSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Gt("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GtAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.GtSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.In("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Le("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LeAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LeSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Lt("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LtAll("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.LtSome("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.Ne("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.NotExists(dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.NotIn("Gavin King", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyEq("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyEqAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGeAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGeSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGt("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGtAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyGtSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyIn("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLeAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLeSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLt("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLtAll("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyLtSome("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyNe("Name", dc);
            NHAssert.IsSerializable(c);
            c = Subqueries.PropertyNotIn("Name", dc);
            NHAssert.IsSerializable(c);
        }