Thanks MTrinder. I've got is sorted now!
Dim fields As New ResultsetFields(2)
fields.DefineField(TripStopPersonActivityFields.PersonId, 0)
fields.DefineField(TripStopPersonActivityFields.PersonActivityTypeId, 1, "OnOffBoard", AggregateFunction.Sum)
Dim relations As IRelationCollection = New RelationCollection()
relations.Add(TripStopPersonActivityEntity.Relations.TripStopEntityUsingTripStopId)
Dim groupByClause As IGroupByCollection = New GroupByCollection()
groupByClause.Add(fields(0))
groupByClause.HavingClause = New PredicateExpression( _
fields(1).SetExpression(TripStopPersonActivityFields.PersonActivityTypeId * 1).SetAggregateFunction(AggregateFunction.Sum) = 1)
Dim filter As IPredicateExpression = ((TripStopFields.LocationId = _tripStop.LocationId) And _
(TripStopFields.StatusId <> StatusEntity.Deleted) And _
(TripStopFields.Etd <= _tripStop.Etd))
Dim dynamicList As New DataTable()
Dim dao As New TypedListDAO()
dao.GetMultiAsDataTable(fields, dynamicList, 0, Nothing, filter, relations, True, groupByClause, Nothing, 0, 0)
lbPersonId.DataSource = dynamicList
lbPersonId.DisplayMember = "PersonId"
lbPersonId.ValueMember = "PersonId"
Which produces:
Query: SELECT [Trip_Stop_Person_Activity].[Person_ID] AS [PersonId], SUM([Trip_Stop_Person_Activity].[Person_Activity_type_ID] * @LLBLEP1) AS [OnOffBoard] FROM ( [Trip_Stop] INNER JOIN [Trip_Stop_Person_Activity] ON [Trip_Stop].[Trip_Stop_ID]=[Trip_Stop_Person_Activity].[Trip_Stop_ID]) WHERE ( ( ( [Trip_Stop].[Location_ID] = @LocationId2 AND [Trip_Stop].[Status_ID] <> @StatusId3) AND [Trip_Stop].[ETD] <= @Etd4)) GROUP BY [Trip_Stop_Person_Activity].[Person_ID] HAVING ( SUM([Trip_Stop_Person_Activity].[Person_Activity_type_ID] * @LLBLEP6) = @PersonActivityTypeId5)
Parameter: @LLBLEP1 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1.
Parameter: @LocationId2 : String. Length: 20. Precision: 0. Scale: 0. Direction: Input. Value: "1-2".
Parameter: @StatusId3 : String. Length: 20. Precision: 0. Scale: 0. Direction: Input. Value: "Deleted".
Parameter: @Etd4 : DateTime. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 16/05/2009 10:05:00.
Parameter: @LLBLEP6 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1.
Parameter: @PersonActivityTypeId5 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1.
I had to enter
TripStopPersonActivityFields.PersonActivityTypeId * 1
rather than just
TripStopPersonActivityFields.PersonActivityTypeId
to avoid "Unable to cast object of type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityField' to type 'SD.LLBLGen.Pro.ORMSupportClasses.IExpression'."
I wonder if there is a neater way around this?