NHibernate RowCountInt64 returns wrong count with transformed query -
i experiencing weird error while executing nhibernate query. have query of type iqueryover<externaluser, externaluser> filtered , transformed (using distinctrootentity, guessing causing problem). create query this:
list<guid> companylist = /* guids */ company company = null; var query = _session.queryover<externaluser>()                     .joinalias(x => x.companies, () => company)                     .where(() => company.id.isin(companylist))                     .transformusing(transformers.distinctrootentity); when execute query.rowcountint64() 4.
when execute query.list() 3 items back.
i have tried query.torowcountint64query().list<long>().sum() gives me 4.
i tried query.torowcountint64query().futurevalue<long>().valuewhich gives me 4.
any ideas how solve this?
i found solution works:
totalcount = query.clone()                   .select(projections.countdistinct<user>(x => x.id))                   .singleordefault<int>(); ...but solution restricts me int32 i'm not happy about. suffice in implementation i'm using it, there might cases elsewhere long needed, other suggestions appreciated.
edit: thing didn't solution above returned int, digging, managed fix projection class:
public class int64countprojection : countprojection {     protected internal int64countprojection(string prop) : base(prop) {}     protected internal int64countprojection(iprojection projection) : base(projection) {}      public override itype[] gettypes(icriteria criteria, icriteriaquery criteriaquery)     {         return new itype[] { nhibernateutil.int64 };     }      public static countprojection distinct<t>(expression<func<t, object>> expression)     {         return new int64countprojection(expressionprocessor.findmemberexpression(expression.body)).setdistinct();     } } ...and class can count (which refined further extension method, enough me):
totalcount = query.clone()                   .select(int64countprojection.distinct<user>(x => x.id))                   .singleordefault<long>(); edit #2 couldn't leave alone, implemented extension method aswell:
     public static long correctrowcount<troot>(this iqueryover<troot> query) troot : ientity      {          return query.clone()                      .select(int64countprojection.distinct<troot>(x => x.id))                      .clearorders()                      .skip(0)                      .take(rowselection.novalue)                      .singleordefault<long>();      } 
Comments
Post a Comment