You might take a look at SOQL polymorphism.
Since the parent object referenced by UserOrGroupId could be either a User or a Group, normal SOQL won't let you reference any parent fields, similar to attempting to reference fields via WhatId on an Event. SOQL's TYPEOF keyword should allow you to perform the query you want.
REVISED
The TYPEOF keyword would still give you the query you want, but I apologize for overlooking it's general unavailability. Without this being a widely available feature, I do not think there is another way to create the single query on GroupMember.
...
Map<Id, Group> groupMap = new Map<Id, Group>([
Select Id, Name
From Group
Where Type = 'Queue'
]);
List<GroupMember> groupMembers = [
Select GroupId, UserOrGroupId
From GroupMember
Where GroupId In :groupMap.keySet()
];
Set<Id> userOrGroupIds = new Set<Id>();
for (GroupMember member : groupMembers) {
userOrGroupIds.add(member.UserOrGroupId);
}
Map<Id, User> userMap = new Map<Id, User>([
Select Id, LastName
From User
Where Id In :userOrGroupIds
]);
Map<Id, List<User>> groupIdToUsers = new Map<Id, List<User>>();
for (Id groupId : groupMap.keySet()) {
groupIdToUsers.put(groupId, new List<User>());
}
for (GroupMember member : groupMembers) {
if (userMap.containsKey(member.UserOrGroupId)) {
groupIdToUsersMap.get(member.GroupId).add(userMap.get(member.UserOrGroupId));
}
}
...
With this, you can loop over the values of groupMap and pull the list of Users from groupIdToUsers.
You can alter your filters and limits in the queries as you see fit, especially if you are expecting a large number of Users. There are also certainly other ways you could write this.
If you want to track which Groups are also members of other Groups, you would need to maintain a separate Group map that gets processed similarly to User map.
for(CustomObj__c record: objects) {
for(Event eventRecord: record.Events) {
// each eventRecord has data
}
}
You can access them individually as an array, too:
objects[0].Events[0].DataField__c
There will never be a conflict, because the child object will be in its own array.
Best Answer
The idea here is that you are first selecting all Contacts with FirstName of Jane here
SELECT OpportunityId from Contact WHERE FirstName = 'Jane'
and pulling the OpportunityId for each of those records... You then feed that into a typical Opportunity query where the Opportunity.AccountId = the Contact.AccountId list you just created with the sub-query above...