开发者

How to project a list with QUeryOver in NHibernate

开发者 https://www.devze.com 2023-04-09 00:50 出处:网络
I am a little mystified(or stupid more likely) when it comes to QueryOver in NHibernate. I am trying something like this:

I am a little mystified(or stupid more likely) when it comes to QueryOver in NHibernate.

I am trying something like this:

_session.QueryOver<Task>()
    .Where(x => x.Category == category)
    .Fetch(x => x.SubTasks).Eager
    .Select(
        x => x.Id,
        x => x.DisplayName,
        x => x.SubTasks)
    .List<object[]>();

Which I would expect would return me a list of arrays of objects with my Id, DisplayName and a collection of some kind of eagerly loaded subtasks.

Instead I get the following exception:

NHibernate.Exceptions.GenericADOException: could not execute query
[ SELECT this_.Id as y0_, this_.DisplayName as y1_, this_.Id as y2_ FROM [Task] this_ WHERE this_.Category = @p0 ]
Positional parameters:  #0>Internal
[SQL: SELECT this_.Id as y0_, this_.DisplayName as y1_, this_.Id as y2_ FROM [Task] this_ WHERE this_.Category = @p0] ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at NHibernate.Loader.Criteria.CriteriaLoader.GetResultColumnOrRow(Object[] row, IResultTransformer resultTransformer, IDataReader rs, ISessionImplementor session) in d:\CSharp\NH\NH开发者_如何学Python\nhibernate\src\NHibernate\Loader\Criteria\CriteriaLoader.cs:line 111
   at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 479
   at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 243
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1694
   --- End of inner exception stack trace ---
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1711
   at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1601
   at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Criteria\CriteriaLoader.cs:line 74
   at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1928
   at NHibernate.Impl.CriteriaImpl.List(IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\CriteriaImpl.cs:line 265
   at NHibernate.Impl.CriteriaImpl.List[T]() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\CriteriaImpl.cs:line 277
   at Codehouse.TimeRegistration.Web.Controllers.TimeRegistrationAjax.TimeRegistrationGetTasksController.Execute(String category) in E:\TfsSource\Codehouse Time Registration Beetle\Main\Source\Codehouse.TimeRegistration.Web.Controllers\TimeRegistrationAjax\TimeRegistrationGetTasksControllers.cs:line 26
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

I have spent a good hour looking on google but to no avail - so any kind of help/pointer would be much appreciated.


Try

_session.QueryOver<Task>()
    .Where(x => x.Category == category)
    .Fetch(x => x.SubTasks).Eager
    .List<Task>()
    .Select(
        x => x.Id,
        x => x.DisplayName,
        x => x.SubTasks)
     .ToList();

I found this to work by creating a List then doing the select after.

0

精彩评论

暂无评论...
验证码 换一张
取 消