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

时间:2011-09-29 08:53:17

标签: c# nhibernate queryover

对于NHibernate中的QueryOver,我有点神秘(或者更有可能是愚蠢的)。

我正在尝试这样的事情:

_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[]>();

我期望它会返回一个包含我的Id,DisplayName和某种急切加载子任务的集合的对象数组列表。

相反,我得到以下异常:

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)

我花了一个小时看谷歌,但没有用 - 所以任何一种帮助/指针都会非常感激。

1 个答案:

答案 0 :(得分:0)

尝试

_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();

我发现这可以通过创建一个List然后进行选择来实现。