function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RadDude89RadDude89 

Comparing values from list using value from another list in apex

Hi,

I'm trying to return values from a list in an apex class using the value from another list.
So I have list1 = [Select Contact__r.ContactEmail from Registrations__c Id in : lstRegistrations]

and what I want to do is check all the records from another object using the value of ContactEmail from above list.
So something like this:
list2=[Select Email from Site__c where Email contains (list1.Contact__r.ContactEmail)]

Can this be done?

Any help is much appreciated.

Thanks

Angello Camacho DragoAngello Camacho Drago
you need to create a list of Strings and then added all the emails of the first list, then you do the soql query, something like this:

list1 = [Select Contact__r.ContactEmail from Registrations__c Id in : lstRegistrations]
List<String> emails = new List<String>();
if(list1.size()>0){
     for(Registrations__c r : list1){
          emails.add(r.Contact__r.ContactEmail);
     }
}
if(emails.size()>0){
      list2=[Select Email from Site__c where Email IN: emails]
}
else{
      //error message
}