List<Account> allAccounts = [SELECT Id FROM Account];
List<Contact> allContacts = [SELECT Id, AccountId FROM Contact];
for (Account a : allAccounts) {
for (Contact c : allContacts) {
if (c.AccountId == a.Id) {
// do work
}
}
}
List<Account> allAccounts = [SELECT Id FROM Account];
List<Contact> allContacts = [SELECT Id, AccountId FROM Contact];
// AccountのIdをキーとして、それに関連するAccountを値とするマップを作成
Map<Id, Account> accountMap = new Map<Id, Account>(allAccounts);
for(Contact con : allContacts){
if(accountMap.containsKey(con.AccountId)) {
// do work
}
}
コメント