NHibernate Query

1) Sql Group by ....mysql

 以前是這麼寫的,由於DateTime是YYYY-MM-DD HH:mm:SS 模式,我只想group 日期。這種寫法再mysql,sqlserver、oracle都沒問題。sql

  var startTime = new DateTime(start.Year, start.Month, start.Day);
            var endTime = new DateTime(end.Year, end.Month, end.Day, 23, 59, 59, 999);
            var count = DetachedCriteria.For<User>("user")
                .SetProjection(
                    Projections.ProjectionList()
                    .Add(Projections.SqlGroupProjection("year(CreateTime)", "year(CreateTime)", new[] { "year" }, new IType[] { NHibernateUtil.Int32 }))
                    .Add(Projections.SqlGroupProjection("Month(CreateTime)", "Month(CreateTime)", new[] { "Month" }, new IType[] { NHibernateUtil.Int32 }))
                    .Add(Projections.SqlGroupProjection("day(CreateTime)", "day(CreateTime)", new[] { "day" }, new IType[] { NHibernateUtil.Int32 }))
                    .Add(Projections.RowCount())
                    )
                .Add(Restrictions.Gt(Projections.Property<User>(s => s.CreateTime), startTime))
                .Add(Restrictions.Le(Projections.Property<User>(s => s.CreateTime), endTime))
                .GetExecutableCriteria(this.CurrentSession)
                .List();
            Dictionary<DateTime, int> dictionary = new Dictionary<DateTime, int>();
            foreach (object[] objects in count)
            {
                dictionary.Add(new DateTime((int) objects[0], (int) objects[1], (int) objects[2]), (int) objects[3]);
            }
            return dictionary;

後來發現還有個能夠作就是 date 方法oracle

Projections.SqlFunction("date", NHibernateUtil.Date, Projections.Property(_=>_.CreateTime);sqlserver

 

2) 一對多查詢this

例如 server

相關文章
相關標籤/搜索