我有以下查询,
SearchTemplate Template = new SearchTemplate();
Template.Criteria = DetachedCriteria.For(typeof(table1));
Template.Criteria.CreateCriteria("table2", "Usr", NHibernate.SqlCommand.JoinType.InnerJoin)
.SetProjection(Projections.ProjectionList()
.Add(Projections.Count("Usr.ID"), "UserCount")
.Add(Projections.GroupProperty("Location"), "ALocation")
.Add(Projections.GroupProperty("Company"), "ACompany")
.Add(Projections.GroupProperty("Usr.Designation"), "ADesignation"));
以上查询为我提供了数据
Location Company Designation Count
Florida A Manager 3
Florida A QA 5
Texas B Manager 6
Texas B QA 7
有没有办法更改查询,以便将数据提供为
Location Company Manager QA
Florida A 3 5
Texas B 6 7
此处Manager和QA是唯一可以进入指定字段的值。
答案 0 :(得分:6)
而不是
.Add(Projections.Count("Usr.ID"), "UserCount")
.Add(Projections.GroupProperty("Usr.Designation"), "ADesignation")
您需要两个投影,每个结果列一个:
.Add(Projections.Sum(Projections.Conditional(
Restrictions.Eq("Usr.Designation", "Manager"),
Projections.Constant(1),
Projections.Constant(0)))
.Add(Projections.Sum(Projections.Conditional(
Restrictions.Eq("Usr.Designation", "QA"),
Projections.Constant(1),
Projections.Constant(0)))