У меня есть
курс 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>())
.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, кто знает что.