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

Invalid initial type LIST;Class__c; for MAP;Id,LIST;Student__c;

Please Suggest,

Here Class__c is Parent
and Student__c is Child 
Child Relationship Name is Students,

Map<Id,List<Student__c>> mapName=new Map<Id,List<Student__c>>([Select Id,(select Name from Students__r) from Class__c]);

Query is working fine in developer console but in apex class, getting error for the above statement.

Your query returns a list of Class__c object records, which is suitable for storing in a type Map<Id, Class__c>.  If you are trying to store a list of student records keyed by class id, you'd need the following:

List<Class__c> classes=[Select Id,(select Name from Students__r) from Class__c];
Map<Id,List<Student__c>> mapName=new Map<Id,List<Student__c>>();
for (Class__c class : classes)
    mapName.put(, class.Students__r);