SOQL: Convert set to list

495    Asked by Natunyadav in SQL Server , Asked on Jul 29, 2021

I am trying to get unique items from an object so I am starting with a set however I need the values to be returned as a list for the visualforce component. I have tried the code below, and variations, but I keep getting errors related to the type not being set correctly. Can anyone help me out? Is there a better way  to convert set to list

and populating a list with unique values from a SOQL query?

public List getItems() { Set license = new Set( [SELECT Name FROM License__c] ); List options = new List(); options.addAll(license); return options; }


Answered by Namberuman dubey

To convert set to list, their is no need to work with lists and sets if you group-by in the query:

public List getItems() { List options = new List(); for(AggregateResult l : [SELECT Name FROM License__c GROUP BY Name]){ options.add(new SelectOption(String.valueOf(l.get('Name')),String.valueOf(l.get('Name')))); } return options; }

Your Answer

Interviews

Parent Categories