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
m@x~W0W~m@x~W0W~ 

Error : Illegal assignment from LIST<Student__c> to SET<Student__c>

public class AsgClass2
{
List<Class__c> clid = [Select id from Class__c];
map<id,Set<Student__c>> newMap = new map<id,Set<Student__c>>();
public map<id, Set<Student__c>> trdata()
{
for(class__c cl:clid)
{
Set<Student__c> stset = [Select id from Student__c where Class__c = :cl.id];
newMap.put(cl.id,stset);
}

return newMap;
}
}

Dhaval PanchalDhaval Panchal

Instead of

Set<Student__c> stset = [Select id from Student__c where Class__c = :cl.id];

Use below


List<Student__c> stset = [Select id from Student__c where Class__c = :cl.id];

souvik9086souvik9086

Do it like this

 

public class AsgClass2
{
List<Class__c> clid = [Select id from Class__c];
map<id,Set<Student__c>> newMap = new map<id,Set<Student__c>>();
public map<id, Set<Student__c>> trdata()
{
for(class__c cl:clid)
{

Set<Student__c> stSet = new Set<Student__c>();
List<Student__c> stLst= new List<Student__c>();

stLst = [Select id from Student__c where Class__c = :cl.id];

stSet.addAll(stLst);
newMap.put(cl.id,stset);
}

return newMap;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

m@x~W0W~m@x~W0W~
Will it remove duplicate entries???
souvik9086souvik9086

Yes sure it will remove duplicates because the list is ultimately added to set and there it will remove duplicates.