+ Start a Discussion
@GM@GM 

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.

Regards,
GM
bob_buzzardbob_buzzard
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.id, class.Students__r);
}