我在 asp.net mvc 网格(特别是telerik)上使用 LINQ to 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)), ), Quote((s, ) => (s.Name)), ), Quote((x, ) => (x.Property)), ), )]
我知道它试图告诉我,我无法急切地获取 Property,因为 MyClass 不在选择中,但问题是 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()
但不知道为什么会这样 我有类似的问题,这解决了它......
关于asp.net-mvc-2 - 选择聚合时如何处理 Linq to NHibernate 的 Fetch 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5251306/