I have a SOQL query result that I use several times in a method. It pulls in many columns that I use for a few things. This includes a relationship column, that I do a query on another object. Is there a more elegant way to get at the relationship ID column than looping through my result. For example:
List<PatronTicket__EventInstance__c> instances =[SELECT
Id,Name,PatronTicket__InstanceDate__c,
PatronTicket__Venue__c
FROM PatronTicket__EventInstance__c
WHERE PatronTicket__TicketableEvent__c = :id];
List<Id> cids = new List<Id>();
for(PatronTicket__EventInstance__c e: instances) {
cids.add(e.PatronTicket__Venue__c);
}
List<Campaign> campaigns = [SELECT Id,Name
FROM Campaign
WHERE Venue__c IN :cids];
I then go on to use both instances
and campaigns
in different contexts. I'm aware I can do another query in the WHERE clause, like:
List<PatronTicket__EventInstance__c> instances =[SELECT
Id,Name,PatronTicket__InstanceDate__c,
PatronTicket__Venue__c
FROM PatronTicket__EventInstance__c
WHERE PatronTicket__TicketableEvent__c = :id];
List<Campaign> campaigns = [SELECT Id,Name
FROM Campaign
WHERE Venue__c IN (
SELECT PatronTicket__Venue__c
FROM PatronTicket__EventInstance__c
WHERE PatronTicket__TicketableEvent__c = :id
)];
But I can't decide if that's more understandable or a more efficient way of doing this query. In this version I have to maintain the criteria twice. What say the hivemind? What's the best way to accomplish this?