Hi,
I have a query that I would like to write that looks like this:
select *
from
lms_internalcoursebookinghistory h
join lms_internalcoursebooking b on h.internalcoursebookingid = b.internalcoursebookingid
where
b.personid = 1
and
h.actiondate in
(select max(h2.actiondate)
from
lms_internalcoursebookinghistory h2
join lms_internalcoursebooking b2 on b2.internalcoursebookingid = h2.internalcoursebookingid
where
b2.personid = 1
group by
h2.internalcoursebookingid
)
after reading through other forum posts and the documentation I've come up with code :
IPredicateExpression subQuery = new PredicateExpression();
subQuery.Add(PredicateFactory.CompareValue(InternalCourseBookingFieldIndex.PersonId, ComparisonOperator.Equal, personID, "h2"));
IRelationCollection relations = new RelationCollection();
relations.Add(InternalCourseBookingEntity.Relations.InternalCourseBookingHistoryEntityUsingInternalCourseBookingId, "h2");
IEntityField2 actionDateInField = EntityFieldFactory.Create(InternalCourseBookingHistoryFieldIndex.ActionDate);
IEntityField2 actionDateMaxField = EntityFieldFactory.Create(InternalCourseBookingHistoryFieldIndex.ActionDate);
actionDateMaxField.Alias = "h2";
IGroupByCollection groupBy = new GroupByCollection();
groupBy.Add(InternalCourseBookingFields.InternalCourseBookingId);
RelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(
new FieldCompareSetPredicate(
actionDateInField,
null,
actionDateMaxField,
null,
SetOperator.In,
subQuery,
relations,
"h2",
100,
null,
false,
groupBy));
filter.PredicateExpression.AddWithAnd(InternalCourseBookingFields.PersonId == personID);
Obviously there are joins on the outer query that I haven't yet written as I haven't been able to get the subquery right yet.
Where have I gone wrong?
Thanks,
Matt