NHibernate Linq查询有投影和计数错误 [英] NHibernate Linq Query with Projection and Count error

查看:167
本文介绍了NHibernate Linq查询有投影和计数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

var list = repositoy.Query<MyClass>.Select(domain => new MyDto()
{ 
    Id = domain.Id,
    StringComma = string.Join(",", domain.MyList.Select(y => y.Name))
});

效果很好:

list.ToList();

但是,如果我尝试获取计数,则会出现异常:

But if I try to get the Count I got an exception:

list.Count();

例外

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException

A recognition error occurred. [.Count[MyDto](.Select[MyClass,MyDto](NHibernate.Linq.NhQueryable`1[MyClass], Quote((domain, ) => (new MyDto()domain.Iddomain.Name.Join(p1, .Select[MyListClass,System.String](domain.MyList, (y, ) => (y.Name), ), ))), ), )]

有什么想法可以在不使用ToList的情况下解决该问题吗?

Any idea how to fix that without using ToList ?

推荐答案

重点是,我们不应在投影上调用Count().这样就可以了

The point is, that we should NOT call Count() over projection. So this will work

var query = repositoy.Query<MyClass>;
var list = query.Select(domain => new MyDto()
{ 
    Id = domain.Id,
    StringComma = string.Join(",", domain.MyList.Select(y => y.Name))
});

var count = query.Count();

当我们使用ICriteria查询时,正确的语法应为

When we use ICriteria query, the proper syntax would be

var criteria = ... // criteria, with WHERE, SELECT, ORDER BY...

// HERE cleaned up, just to contain WHERE clause
var totalCountCriteria = CriteriaTransformer.TransformToRowCount(criteria);

因此,对于 Count -使用最简单的查询,即包含相同的JOIN和WHERE部分

So, for Count - use the most simple query, i.e. containing the same JOINs and WHERE part

这篇关于NHibernate Linq查询有投影和计数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆