如何设置NHibernate标准来执行此类查询?
SELECT
COUNT(1) AS CNT,
l.CAMPAIGN_ID AS CAMPAIGN_ID,
MAX(mc.NAME) AS CAMPAIGN_NAME
FROM
SA_LEADS l
INNER JOIN
SA_CAMPAIGNS mc
ON
l.CAMPAIGN_ID = mc.ID
GROUP BY
CAMPAIGN_SOURCED_ID
ORDER BY
CNT DESC
我有两个表SA_LEADS
和SA_CAMPAIGNS
的映射。
答案 0 :(得分:2)
对我来说,我会从我试图分组的元素开始,所以我将从一开始 活动的基础。我无法从您的查询中看出,但我认为您的映射有一种从广告系列转移到销售线索的方式。我假设这被称为“SALES_LEAD_LIST”
我还想制作一个小班来接收投影结果,这样就可以创建一些东西来保存结果。
public class CAMPAIGN_PERFORMANCE
{
public CAMPAIGN_PERFORMANCE() {}
public int CP_ID {get; set;}
public string CP_NAME {get; set;}
public int CP_NO_OF_SALES_LEADS {get; set;}
}
一旦你有了投射结果的东西,你可以创建一个标准的标准,然后通过投影将它推入你的新课程
ICriteria criteria = base.Session.CreateCriteria(typeof(SA_CAMPAIGNS));
criteria.CreateAlias("SALES_LEADS_LIST", "SA_LEADS", JoinType.InnerJoin);
criteria.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("ID"), "CP_ID")
.Add(Projections.Property("CAMPAIGN_NAME"), "CP_NAME")
.Add(Projections.CountDistinct("SA_LEADS.ID"), "CP_NO_OF_SALES_LEADS")
.Add(Projections.GroupProperty("ID"));
.Add(Projections.GroupProperty("CAMPAIGN_NAME")));
IList<CAMPAIGN_PERFORMANCE> cpProjections = criteria
.SetResultTransformer(
new AliasToBeanResultTransformer(typeof(CAMPAIGN_PERFORMANCE)))
.List<CAMPAIGN_PERFORMANCE>();