Fleming
Fleming

Reputation: 101

LINQ - Help me grasp it with an example I think LINQ should be able to solve!

I am trying to get into LINQ to objects as I can see the power of it. Lucky enough I have a question that I think LINQ should be able to solve.

Here is the question (the details are an example);

public class SchoolClass
{
    public int ID;
    public string Name;
    public string Teacher;
    public string RoomName;

    public string Student_Name;
    public int Student_Age;
}

As you can see by the example, there is a one to many relationship between the ClassName, Teacher and Room and the Students, i.e. there are potentially many students in the one class.

If we have a List is it possible using LINQ to create a List but have only one instance ID, Name, Teacher, RoomName and an ArrayList of Student_Name and Age?

Producing this:

public class Students
{
    public string Student_Name;
    public int Student_Age;
}

public class SchoolClass
{
    public int ID;
    public string Name;
    public string Teacher;
    public string RoomName;

    public ArrayList Students;
}

Essentially, using LINQ to clean the List to a more logical structure?

To give some background to this example. The second structure is used by a DataGrid to produce a Master-Child relationship. We store SchoolClass and StudentInformation in classes as shown above. It would be good use of LINQ to be able to convert our initial List into a structure which can be used by the DataGrid.

Upvotes: 4

Views: 201

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062975

I changed the ArrayList to List<Students>, and:

    List<SourceData> source = new List<SourceData>();
    //...your data here ;-p
    var classes = (from row in source
                  group row by new {
                          row.ID, row.Name,
                          row.Teacher, row.RoomName }
                      into grp
                      select new SchoolClass
                      {
                          ID = grp.Key.ID,
                          Name = grp.Key.Name,
                          Teacher = grp.Key.Teacher,
                          RoomName = grp.Key.RoomName,
                          Students = new List<Students>(
                              from row in grp
                              select new Students
                              {
                                  Student_Age = row.Student_Age,
                                  Student_Name = row.Student_Name
                              })
                      }).ToList();

Upvotes: 6

Lee D
Lee D

Reputation: 12961

If I'm understanding this correctly, I would've thought the best way to implement the SchoolClass class would be to create a Student class (probably a LINQ-to-SQL entity, if you're using it) and to have a generic list of type student, something similar to this:

public class SchoolClass
{    
    public int ID;    
    public string Name;    
    public string Teacher;    
    public string RoomName;    
    public List<Student> Students;
}

The list of students could then be populated using a linq query, although I'm not sure exactly how without more information.

Hope this is some help.

Upvotes: 1

Related Questions