所以我有一個nhibernate icriteria查詢獲取我想要的結果列表。nhibernate使用icriteria與一組和有,並獲得總行數(C#)
但是我不知道如何獲得總行數。
public IEnumerable<SpecialismCombo> List(SpecialismListCriteria criteria, out int total)
{
//run the sub query
ICriteria countAgencies = m_SpecialismComboRepository.QueryAlias("sc");
if (!String.IsNullOrEmpty(criteria.AgencyIds) && !criteria.AgencyIds.Equals("0"))
{
List<int> AgencyIds = criteria.AgencyIds.Split(new char[] { ',' }).ToList<int>();
countAgencies.CreateAlias("Agencies", "a", NHibernate.SqlCommand.JoinType.InnerJoin);
countAgencies.Add(Restrictions.In("a.AgencyId", AgencyIds));
}
total = Convert.ToInt32(
countAgencies.SetProjection(
Projections.CountDistinct("a.AgencyId")
).UniqueResult()
);
//create query
ICriteria query = m_SpecialismComboRepository.QueryAlias("sc");
if (!String.IsNullOrEmpty(criteria.AgencyIds) && !criteria.AgencyIds.Equals("0"))
{
List<int> AgencyIds = criteria.AgencyIds.Split(new char[] { ',' }).ToList<int>();
query.CreateAlias("Agencies", "a", NHibernate.SqlCommand.JoinType.InnerJoin);
query.Add(Restrictions.In("a.AgencyId", AgencyIds));
}
query.Add(Restrictions.Eq(Projections.Count("SpecialismComboId"),total));
ProjectionList pl = Projections.ProjectionList()
.AddPropertyAlias("SpecialismComboId", "SpecialismComboId")
.AddPropertyAlias("SpecialismComboDisplayText", "SpecialismComboDisplayText")
.AddPropertyAlias("SpecialismComboDisciplineDisplayText", "SpecialismComboDisciplineDisplayText")
.AddPropertyAlias("SpecialismComboIdText", "SpecialismComboIdText")
.Add(Projections.GroupProperty("SpecialismComboId"));
// set the right total for records being returned
total = query.ToRowCount();
// Return the query results
return query
.SetProjection(Projections.Distinct(
pl
))
.SetResultTransformer(new KnownPropertyAliasToBeanResultTransformer(typeof(SpecialismCombo)))
.SetFirstResult(criteria.FirstRecord)
.SetMaxResults(criteria.PageSize)
.List<SpecialismCombo>() as List<SpecialismCombo>;
}
我試圖用一個ICriteriaExtension我們:
public static int ToRowCount(this ICriteria query)
{
return query.ToRowCountQuery().UniqueResult<int>();
}
但它返回null。
所以在SQL生成的查詢是:
exec sp_executesql N'SELECT distinct top 15 this_.SpecialismComboId as y0_,
(select dbo.fn_GetSpecialismComboDisplayText(this_.SpecialismComboId,''/'')) as y1_,
(select dbo.fn_GetSpecialismComboDisciplineDisplayText(this_.SpecialismComboId,'', '')) as y2_,
(select dbo.fn_GetSpecialismComboIdText(this_.SpecialismComboId,''|'')) as y3_,
this_.SpecialismComboId as y4_
FROM dbo.SpecialismCombo this_
inner join AgencySpecialismCombo agencies3_ on this_.SpecialismComboId=agencies3_.SpecialismComboId
inner join dbo.Agency a1_ on agencies3_.AgencyId=a1_.AgencyId WHERE a1_.AgencyId in (@p0)
GROUP BY this_.SpecialismComboId HAVING count(this_.SpecialismComboId) = @p1',N'@p0 int,@p1 int',@p0=4741,@p1=1
行數根據我上面的代碼是
exec sp_executesql N'SELECT top 2147483647 count(*) as y0_
FROM dbo.SpecialismCombo this_
inner join AgencySpecialismCombo agencies3_ on this_.SpecialismComboId=agencies3_.SpecialismComboId
inner join dbo.Agency a1_ on agencies3_.AgencyId=a1_.AgencyId WHERE a1_.AgencyId in (@p0)
HAVING count(this_.SpecialismComboId) = @p1',N'@p0 int,@p1 int',@p0=4741,@p1=1
但我需要它是象是這樣的:
exec sp_executesql N'SELECT sum(idcount) from
(select count(*) idcount
FROM dbo.SpecialismCombo this_
inner join AgencySpecialismCombo agencies3_ on this_.SpecialismComboId=agencies3_.SpecialismComboId
inner join dbo.Agency a1_ on agencies3_.AgencyId=a1_.AgencyId WHERE a1_.AgencyId in (@p0)
GROUP BY this_.SpecialismComboId HAVING count(this_.SpecialismComboId) = @p1) as sub
',N'@p0 int,@p1 int',@p0=4741,@p1=1
但我再次無法弄清楚如何使用icriteria(這是我們的數據訪問標準)來得到這個查詢和公司正確的計數正在返回!
任何想法,將不勝感激:)
使用功能NHibernate 2.0,我相信。
謝謝!
感謝您的答覆。看起來令人失望的是,我有一個查詢返回我想要的記錄..但我無法輕鬆獲得查詢記錄的數量,而無需編寫其他函數或代碼。但也許這只是icriteria的限制! – Jen