Let’s check with some examples,
- n
- Using Map
n
List results = Database.query('Select Id From Account Limit 5');nSet accIds = (new Map(results)).keyset();
In second line we are creating new Map
- n
- One Liner (Without Dynamic SOQL)
n
Set accIds = (new Map([Select Id from Account Limit 5])).keyset();
- n
- One Liner (With Dynamic SOQL)
n
String sQuery = 'Select Id from Account Limit 5';nSet accIds = (new Map((List)Database.Query(sQuery))).keySet();