简体   繁体   English

如何在 NHibernate 中使用 QUeryOver 投影列表

[英]How to project a list with QUeryOver in NHibernate

I am a little mystified(or stupid more likely) when it comes to QueryOver in NHibernate.当谈到 NHibernate 中的 QueryOver 时,我有点困惑(或者更可能是愚蠢)。

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.我期望它会返回一个对象数组列表,其中包含我的 Id、DisplayName 和某种急切加载的子任务的集合。

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\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.我发现这是通过创建一个列表然后在之后进行选择来工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM