NHibernate ICriteria根据子句对组进行计数



我正在尝试计算Groupby子句产生的查询组,但我似乎无法使用ICriteria获得每个组的计数。

我的实体:

public class PageView
{
#region public fields
public virtual int PagelogID { get; set; }
public virtual string PageTitle { get; set; }
public virtual string UserID { get; set; }
public virtual DateTime DateViewed{get;set;}  --->Map to Date_Viewed column
public virtual string SessionID { get; set; }
public virtual string UserGroup { get; set; }
#endregion
}

我想根据DateViewed按年/月对数据进行分组。这样的结果可以通过以下查询来实现:

select extract(year from DATE_VIEWED) "year", extract(month from DATE_VIEWED) "month", count(*) from PAGEVIEW_LOG where date_viewed > to_date(add_months(sysdate,-12)) 
group by extract(year from DATE_VIEWED), extract(month from DATE_VIEWED)
order by "year", "month" desc;

这是我的标准:

ProjectionList pl = Projections.ProjectionList();
this.crit.Add(Expression.Gt("DateViewed", DateTime.Now.AddMonths(-12)));
pl.Add(Projections.SqlGroupProjection(
"Extract(month from Date_Viewed) MONTH",
"Extract(month from Date_Viewed)",
new String[] { "MONTH" },
new NHibernate.Type.IType[] { NHibernateUtil.Int32 }), "MONTH");
pl.Add(Projections.SqlGroupProjection(
"Extract(year from Date_Viewed) YEAR",
"Extract(year from Date_Viewed)",
new String[] { "YEAR" },
new NHibernate.Type.IType[] { NHibernateUtil.Int32 }), "YEAR");
//pl.Add(Projections.Count("*"));
this.crit.SetProjection(pl);
this.crit.AddOrder(Order.Desc("YEAR"));
this.crit.AddOrder(Order.Desc("MONTH"));

这成功地给了我想要的一切,除了">计数(*(";查询的一部分。

我尝试过的:

1:添加p1.Add(Projections.Count("YEAR"):

这给了我一个错误,因为尽管我给了它一个别名,但Nhibernate并没有将YEAR识别为列。

2:添加p1.Add(Projections.Count("*"):

与上述结果相同

3:将SqlGroupProjectionfor year列存储到变量IProjection yearProjection中,并添加**p1.Add(Projections.Count(yearProjection)这给了我错误的";长度必须大于0〃;错误

4:pl.Add(Projections.Count(Projections.RowCount()))也没有运气

我在这里错过了什么?如何将Count(*(添加到标准中?

我非常接近:

代替做:pl.Add(Projections.Count(Projections.RowCount()))

这应该是:pl.Add(Projections.RowCount())

相关内容

  • 没有找到相关文章

最新更新