加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-2 – 在选择聚合时,如何处理Linq到NHibernate的Fetc

发布时间:2020-12-16 04:05:58 所属栏目:asp.Net 来源:网络整理
导读:我在asp.net mvc Grid(具体是telerik)上使用LINQ到NHibernate的IQueryable实现,我知道我需要为这个特定的网格急切地获取一些东西. 所以我的查询看起来像这样: var query = from s in repository.QueryMyClass() orderby s.Property.Name,s.Name select s;qu
我在asp.net mvc Grid(具体是telerik)上使用LINQ到NHibernate的IQueryable实现,我知道我需要为这个特定的网格急切地获取一些东西.

所以我的查询看起来像这样:

var query =  from s in repository.Query<MyClass>()
                     orderby s.Property.Name,s.Name
                     select s;

query = query.Fetch(x => x.Property);

现在,如果我执行query.ToList(),一切都很好,我可以验证它在集成测试中是否有效.

这很棒.

但是,如果我执行query.Count()或其他聚合查询的东西,我会得到一个异常:

Query specified join fetching,but the
owner of the fetched association was
not present in the select list
[FromElement{explicit,not a collection
join,fetch join,fetch non-lazy
properties,classAlias=0,role=,tableName=[Property],tableAlias=property1,origin=MyClass
myclass0_,colums={myclass0_.PropertyGuid
,className=Property}}]
[.Count(.Fetch(.ThenBy(.OrderBy(NHibernate.Linq.NhQueryable`1[MyClass],
Quote((s,) => (s.Property.Name)),),) => (s.Name)),Quote((x,
) => (x.Property)),)]

我知道它试图告诉我,我不能急于获取Property,因为MyClass不在select中,但问题是Count()实际上是通过Grid调用的,并且从我的代码外部处理.

我应该做的就是给网格一个IQueryable,它应该能够自己处理分页,排序等.

还有其他人不得不用NHibernate Fetching解决这个问题,你是如何解决它的?

解决方法

var query =  from s in repository.Query<MyClass>()
                     orderby s.Property.Name,s.Name
                     select s;
query = query.Fetch(x => x.Property).ToList();

然后你可以去做

query.Count()

它应该处于正常运行状态.

至于为什么我怀疑这是可以做的事情

AsEnumerable()

要么

AsQueryable()

但不知道为什么会这样我有类似的问题,这解决了它……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读