I am trying and so far not succeeding in making a query with a group by-statement.
I have a table with different types of activities (tblExcursion) and a table with the planned (tblReservationDetail). In tblExcursion I have a column ‘GroupName’. There are multiple activities under one GroupName (e.g. kayak, sailing, waterpolo is groupname Watersports).
Now I want to retrieve the number of participants per GroupName (not per activity/excursion!). But I am not getting there with .GroupBy() or group…by
This is the query where I use the activityname and not the groupname:
var vAllExcursions = (from oExcursion in clsApplication._oDBConnection.tblExcursions
select oExcursion).ToList();
foreach (tblExcursion EXitem in vAllExcursions)
{
var vAllExcursionsPerType = (from oReservationDetail in clsApplication._oDBConnection.tblReservationDetails
where oReservationDetail.StartTime.Date.Year == this.cboYear.getSelectedID()
&& oReservationDetail.StartTime.Date.Month == this.cboMonth.getSelectedID()
&& oReservationDetail.ExcursionID == EXitem.ID
select oReservationDetail).ToList();
}
Here I fill my datagrid
You want to group by the
GroupNameto get the groups:Then in your inner loop, find any related reservations by checking if the
ExcursionIDmatches any id in the list associated with the group: