【发布时间】:2016-07-14 13:17:21
【问题描述】:
假设我有这个简单的n-n 表(在people 和product 之间):
//id people_id product_id
1 1 1
2 1 3
3 1 5
4 2 1
还有这个类(已经映射):
public class PeopleProduct
{
public virtual int TableId { get; set; } //Mapped to id
public virtual int PeopleId { get; set; } //Mapped to people_id
public virtual Product Product { get; set; } //Mapped to product_id
}
如您所见,有两个 people,第一个有 3 个 products,第二个只有 1 个。
如何使用 CreateCriteria 获取唯一 people_id 的计数?
我目前正在尝试使用这个:
var crit = StatelessSession.CreateCriteria<PeopleProduct>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.Count<PeopleProduct>(c => c.PeopleId))
.Add(Projections.Group<PeopleProduct>(g => g.PeopleId)));
var count = Convert.ToInt64(crit.UniqueResult());
但它总是返回一个带有 [count, id] 数组的列表:
[3, 1] and [2, 1]
这不是最好的结果,因为这个表可能会返回数千个people_id。
【问题讨论】:
标签: nhibernate fluent-nhibernate nhibernate-criteria rowcount