Question

I have a list of ClassRooms and then I get a list of students inside that classroom. Here in this method, I create a list of ClassRoom first. then I get the list of students in that classroom. Now when I am trying to add the list of students to the first list. I get this error.

The best overload method match for List... has invalid arguments

public static List<MyList> getRecords()
{
    int id = 10;
    int id2 = 20;
    List<MyList> ClassRooms = GetClassRooms(id);
    foreach(MyList Students in ClassRooms)
    {
        List<MyList> studentProfile = GetStudentProfile(id, Students.ID);
        foreach(MyList studentDetail in studentProfile) ClassRooms.Add(studentDetail);
    }   
    return ClassRooms;
}

What am I doing wrong?

UPDATE -

public class MyList
{
    public int ID;
    public string Class Number;
    public bool Active;
    public int StudentID;
    public string FirstName;
    public string LastName;
    public int TotalStudents;
}
Was it helpful?

Solution

You may want to rethink your class structure and variable names. I'm not sure what MyList is supposed to represent, a student or a classroom. You can solve the problem at hand with a Select statement, though:

List<MyList> ClassRooms = GetClassRooms(id)
    .Select(classroom => GetStudentProfile(id, classroom.ID));
return ClassRooms;

This will take each class in the list of classrooms, call GetStudentProfile on it, and add that to the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top