I have this
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>())
.Take(QueryLimits.Tasks)
.List<TaskAppointments>().ToList();
This works but gives me duplicate rows back due to joining.
So I tried
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();
Note .TransformUsing(Transformers.DistinctRootEntity)
When I try to run it with the above line I get this error
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:
Seems like all of a sudden it ignores my Select statement and tries to put "TEST$!$" what should be put in TaskName into who knows what.
精彩评论