最终得到以下内容以供将来参考:
var startDate = DateTime.Now.AddMonths(-6);
startDate = new DateTime(startDate.Year,startDate.Month,1);
var jobs = _session.QueryOver<Job>()
.WhereRestrictionOn(c => c.DateCreated).IsBetween(startDate).And(DateTime.Now)
.SelectList(list => list
.Select(Projections.SqlGroupProjection(
"YEAR(DateCreated) As [Year]",
"YEAR(DateCreated)",
new[] { "YEAR" },
new IType[]{NHibernateUtil.Int32}))
.Select(Projections.SqlGroupProjection(
"MONTH(DateCreated) As [Month]",
"MONTH(DateCreated)",
new[] { "Month" },
new IType[] { NHibernateUtil.Int32 }))
.SelectCount(x=>x.Id))
.OrderBy(Projections.SqlFunction(
"YEAR",
NHibernateUtil.Int32,
Projections.Property<Job>(item=>item.DateCreated))).Desc
.ThenBy(Projections.SqlFunction(
"MONTH",
NHibernateUtil.Int32,
Projections.Property<Job>(item => item.DateCreated))).Desc
.List<object>();
其中产生:
SELECT YEAR(DateCreated) As [Year],
MONTH(DateCreated) As [Month],
count(this_.Id) as y2_
FROM Jobs this_
WHERE this_.DateCreated between '2011-11-01T00:00:00.00' /* @p0 */ and '2012-05-07T14:23:52.00' /* @p1 */
GROUP BY YEAR(DateCreated),
MONTH(DateCreated)
ORDER BY datepart(year, this_.DateCreated) desc,
datepart(month, this_.DateCreated) desc