Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nhibernate ToRowCountQuery似乎忽略了分组_Nhibernate_Telerik Mvc_Queryover_Future_Rowcount - Fatal编程技术网

Nhibernate ToRowCountQuery似乎忽略了分组

Nhibernate ToRowCountQuery似乎忽略了分组,nhibernate,telerik-mvc,queryover,future,rowcount,Nhibernate,Telerik Mvc,Queryover,Future,Rowcount,我试图从常规查询创建一个rowcount查询,但是生成的SQL似乎由于导致错误的计数而缺少组。有人知道我做错了什么吗 首先是询问: var query = Session.QueryOver<InkoopFactuurListItem>() .Where(i => i.KlantId == Klant.Id) .AndRestrictionOn(i => i.Status).IsIn(statussen) .SelectList(list =>

我试图从常规查询创建一个rowcount查询,但是生成的SQL似乎由于导致错误的计数而缺少组。有人知道我做错了什么吗

首先是询问:

var query = Session.QueryOver<InkoopFactuurListItem>()
    .Where(i => i.KlantId == Klant.Id)
    .AndRestrictionOn(i => i.Status).IsIn(statussen)
    .SelectList(list => list
        .SelectGroup(h => h.Id).WithAlias(() => dto.Id)
        .SelectGroup(h => h.Banknummer).WithAlias(() => dto.Banknummer)
        .SelectGroup(h => h.CrediteurNaam).WithAlias(() => dto.CrediteurNaam)
        .SelectGroup(h => h.DienstType).WithAlias(() => dto.DienstType)
        .SelectGroup(h => h.DocumentId).WithAlias(() => dto.DocumentId)
        .SelectGroup(h => h.DocumentNaam).WithAlias(() => dto.DocumentNaam)
        .SelectGroup(h => h.Factuurbedrag).WithAlias(() => dto.Factuurbedrag)
        .SelectGroup(h => h.Klantnummer).WithAlias(() => dto.Klantnummer)
        .SelectGroup(h => h.Factuurbtw).WithAlias(() => dto.Factuurbtw)
        .SelectGroup(h => h.FactuurDatum).WithAlias(() => dto.FactuurDatum)
        .SelectGroup(h => h.Factuurnummer).WithAlias(() => dto.Factuurnummer)
        .SelectGroup(h => h.IMSNummer).WithAlias(() => dto.IMSNummer)
        .SelectGroup(h => h.KlantId).WithAlias(() => dto.KlantId)
        .SelectGroup(h => h.Soortfactuur).WithAlias(() => dto.Soortfactuur)
        .SelectGroup(h => h.Status).WithAlias(() => dto.Status)
        .SelectGroup(h => h.VerwerktOp).WithAlias(() => dto.VerwerktOp)
        .SelectMin(h => h.Van).WithAlias(() => dto.Van)
        .SelectMax(h => h.Tot).WithAlias(() => dto.Tot))
    .TransformUsing(Transformers.AliasToBean<InkoopFactuurListItem>());

var rowcount = query.ToRowCountQuery().FutureValue<int>();

IEnumerable<InkoopFactuurListItem> results;
if (command.Page > 0 && command.PageSize > 0)
{
    results = query
        .Skip((command.Page - 1) * command.PageSize)
        .Take(command.PageSize).Future<InkoopFactuurListItem>();
}
else
{
    results = query
        .Take(command.PageSize)
        .Future<InkoopFactuurListItem>();
}

count = rowcount.Value;
那么groupby在rowcount查询中的位置是什么

更新 事实证明,ToRowCountQuery从原始查询中剥离了选择和分组。那么我该怎么做呢

select count(*)
from (... query ...)
Firo的回答似乎是朝着正确的方向迈出的一步,但我不能使用CountDistinct,因为我需要所有字段的计数,包括选择列表中显示的最小/最大字段。其次,CountDistinct不将IProjection作为参数:

//
// Summary:
//     A distinct property value count
public static CountProjection CountDistinct(Expression<Func<object>> expression);
//
// Summary:
//     A distinct property value count
public static CountProjection CountDistinct<T>(Expression<Func<T, object>> expression);
//
// Summary:
//     A distinct property value count
//
// Parameters:
//   propertyName:
public static CountProjection CountDistinct(string propertyName);
但是,这会导致一个
ArgumentOutOfRangeException

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
编辑:

ToRowCountQuery
删除所有投影,甚至分组。如果您自己运行查询,您将看到
count(*)
在使用
groupby
的查询中,将返回每个组的计数,而不是组数。您必须对所有组列进行计数,以获得组数

您需要从(查询)中选择计数(*)之类的内容,这是不可能直接实现的。 我只能想到一个脆弱的解决方案,在这个解决方案中,您可以生成如下所示的sql,然后

stringsql=GenerateSQL(query.underyingcriteria);
rowcount=session.CreateSQLQuery(string.Format(“SELECT Count(*)FROM({0})”,sql)).SetParameter(“?”,KlantId).FutureValue();

当我向右滚动到火星轨道附近的某个地方时,我看到一个
群组。这不是你想要的?如果你仔细看,你看到的不是1个而是2个查询。一个有一组,一个没有。他们都应该有一个小组,好的!容易被忽视。第一个查询仅对子查询中的条件进行一次计数,以获取
TOP
子句的标量值。它不应该分组。格特,这正是我的问题。为什么它不分组呢?!分组通过selectlist在主查询中,所以不应该也在rowcount查询中吗?我想,这就是原因:@RichardBrown回答了同样的问题“很抱歉花了这么长时间才回到这个问题。不幸的是,我不认为这个查询可以用ICriteria编写,这最终意味着它也不能在QueryOver中完成。”Firo,这看起来像是我需要的,但我如何才能一次性将分页查询和行数查询发送到数据库?这就是ToRowCount和使用Futures背后的全部想法。请注意,由于Min和Max函数,我不能只计算单个字段。我不明白你的建议怎么做。Firo,CountDistinct不把IProjection作为论据。啊,对了,对不起,这是另一个类似的问题,同样的问题,我接受这个作为答案,因为它回答了我最初的问题,关于ToRowCountQuery中的分组发生了什么事。我将创建一个关于计算子查询中具有复杂分组的行的新问题,因为在这里还没有被询问和回答。
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
string sql = GenerateSQL(query.UnderlyingCriteria);
rowcount = session.CreateSQLQuery(string.Format("SELECT Count(*) FROM ({0})", sql)).SetParameter("???", KlantId).FutureValue<int>();