为什么TransformUsing(Transformers.DistinctRootEntity)在我的语句中不起作用?

时间:2011-07-04 22:03:50

标签: nhibernate queryover

我有这个

课程cAlias = null;         任务tAlias = null;         CoursePermission cpAlias = null;         TaskAppointments taskAppointments = null;

    List<TaskAppointments> result = session.QueryOver<Task>(() => tAlias)
              .JoinAlias(() => tAlias.Course, () => cAlias)
              .JoinAlias(() => cAlias.CoursePermissions, () => cpAlias)
              .Where(Restrictions.In(Projections.Property(() => cAlias.Id), courseIds))
              .And(x => x.DueDate >= startDate)
              .And(x => x.DueDate <= endDate)
              .Select(Projections.Property(() => tAlias.TaskId).WithAlias(() => taskAppointments.TaskId),
                      Projections.Property(() => cpAlias.BackgroundColor).WithAlias(() => taskAppointments.Color),
                      Projections.Property(() => tAlias.DueDate).WithAlias(() => taskAppointments.DueDate),
                      Projections.Property(() => tAlias.TaskName).WithAlias(() => taskAppointments.TaskName))
              .TransformUsing(Transformers.AliasToBean<TaskAppointments>())
              .Take(QueryLimits.Tasks)
              .List<TaskAppointments>().ToList();

这样可行,但由于加入而给我重复的行。

所以我试过

        Course cAlias = null;
    Task tAlias = null;
    CoursePermission cpAlias = null;
    TaskAppointments taskAppointments = null;

    List<TaskAppointments> result = session.QueryOver<Task>(() => tAlias)
              .JoinAlias(() => tAlias.Course, () => cAlias)
              .JoinAlias(() => cAlias.CoursePermissions, () => cpAlias)
              .Where(Restrictions.In(Projections.Property(() => cAlias.Id), courseIds))
              .And(x => x.DueDate >= startDate)
              .And(x => x.DueDate <= endDate)
              .Select(Projections.Property(() => tAlias.TaskId).WithAlias(() => taskAppointments.TaskId),
                      Projections.Property(() => cpAlias.BackgroundColor).WithAlias(() => taskAppointments.Color),
                      Projections.Property(() => tAlias.DueDate).WithAlias(() => taskAppointments.DueDate),
                      Projections.Property(() => tAlias.TaskName).WithAlias(() => taskAppointments.TaskName))
              .TransformUsing(Transformers.AliasToBean<TaskAppointments>())
              .TransformUsing(Transformers.DistinctRootEntity)
              .Take(QueryLimits.Tasks)
              .List<TaskAppointments>().ToList();

注意.TransformUsing(Transformers.DistinctRootEntity)

当我尝试使用上面的行运行时,我收到此错误

NHibernate.Exceptions.GenericADOException was caught
  Message=Unable to perform find[SQL: SQL not available]
  Source=NHibernate
  SqlString=SQL not available
  StackTrace:
       at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
       at NHibernate.Impl.CriteriaImpl.List(IList results)
       at NHibernate.Impl.CriteriaImpl.List[T]()
       at NHibernate.Criterion.QueryOver`1.List[U]()
       at NHibernate.Criterion.QueryOver`1.NHibernate.IQueryOver<TRoot>.List[U]()
       at GetTasksForDateRange(DateTime startDate, DateTime endDate, List`1 courseIds, Student student) in TaskRepo.cs:line 134
       at GetTasksByDateRange(Int32 start, Int32 end, String email) in CalendarService.cs:line 150
  InnerException: System.ArgumentException
       Message=The value "TEST$!$" is not of type "TaskAppointments" and cannot be used in this generic collection.
Parameter name: value
       Source=mscorlib
       ParamName=value
       StackTrace:
            at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, Type targetType)
            at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item)
            at NHibernate.Util.ArrayHelper.AddAll(IList to, IList from)
            at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
       InnerException: 

似乎突然间它忽略了我的Select语句,并尝试将“TEST $!$”放入TaskName中,以便知道什么。

0 个答案:

没有答案