+ Start a Discussion
HydHyd 

Reg: Distinct values from the object

Hi,

 

My object is : student__c with custom fields name,city__c.

It has 4 records.

In city__c the values like Hyd,Bang,Hyd,Bang

when i get these values into picklist all 4 values are displaying.

But i would like to display only (Hyd,Bang) distinct values in picklist .

 

I wrote the below code..

 

 

public class vvnr {
    List<Student__c> obj;
    
    public List<selectOption>  getLstdata() 
    {
        List<selectOption> options=new List<selectOption>();
        options.add(new SelectOption('--None--','--None--'));
        
        for(Student__c obj: [select  name,city__c from student__c ])
        {
           options.add(new selectOption(obj.city__c,obj.city__c));       
        }
        return options;
        
    }
    public String lst { get; set; }
}

 

 

 

can any one suggest me....!

 

Thanks....

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

Try the sample code given below:

 

public List<selectOption>  getLstdata()

    {

        List<selectOption> options=new List<selectOption>();

        options.add(new SelectOption('--None--','--None--'));

 

 

                Set<String> city= new Set<String>();

        for(Student__c obj: [select  name,city__c from student__c ])

        {

                 if(!city.contains(obj.city__c))

                 {

                    city.add(obj.city__C);

                    options.add(new selectOption(obj.city__c,obj.city__c));      

                  }

        }

        return options;

 

    }

All Answers

Pradeep_NavatarPradeep_Navatar

Try the sample code given below:

 

public List<selectOption>  getLstdata()

    {

        List<selectOption> options=new List<selectOption>();

        options.add(new SelectOption('--None--','--None--'));

 

 

                Set<String> city= new Set<String>();

        for(Student__c obj: [select  name,city__c from student__c ])

        {

                 if(!city.contains(obj.city__c))

                 {

                    city.add(obj.city__C);

                    options.add(new selectOption(obj.city__c,obj.city__c));      

                  }

        }

        return options;

 

    }

This was selected as the best answer
HydHyd

I got it.....

Thanks .