user2818430
user2818430

Reputation: 6029

List<Object> to hierarchy

I have a List<Data> where Data is

public class Data
{
  public string SchoolId {get; set;}
  public string SchoolName {get; set;}
  public string TeacherId {get; set;}
  public string TeacherName {get; set;}
}

The List is flat:

SchoolId    SchoolName     TeacherId      TeacherName
1           X              1              Mr X
1           X              2              Mrs Y
2           Y              3              Mr Z
2           Y              1              Mr X

So basically a teacher can belong to many schools.

How can I convert the flat list above into a List<School>

public class School
{
     public School()
     {
          this.Teachers  = new List<Teacher>();
     }
     public string SchoolId { get; set; }
     public string SchoolName { get; set; }
     public List<Teacher> Teachers {get; set;}
}  


public class Teacher
{
    public string TeacherId { get; set; }
    public string TeacherName { get; set; }
} 

Upvotes: 1

Views: 439

Answers (4)

Alex Aparin
Alex Aparin

Reputation: 4522

item.SchoolIdNote: it is pseudo code (for brief code I used constructors with parameters instead usage of Properties)

List<Data> data = //....;
Dictionary<string, Teacher> teachers = new Dictionary<string, Teacher>();
Dictionary<string, School> schools = new Dictionary<string, School>();
foreach (var item in data)
{
   if (item.TeacherId not in teachers)
      teachers.add(item.TeacherId, new Teacher(item.TeacherId, item.TeacherName));
}
foreach (var item in data)
{
   if (item.SchoolId not in schools)
      schools.add(item.SchoolId , item.SchoolName, new School(item.SchoolId , teachers[item.SchoolId]));
}
List<School> gen_schools =     // get values from schools;

P.S. Actually you use wrong representation of your database (you should separate teachers from schools into two different tables).

Upvotes: 0

Hamid Pourjam
Hamid Pourjam

Reputation: 20764

The order of this approach is O(n^2) and it does not share teacher instances.

var schools = data
    .GroupBy(x => x.SchoolId)
    .Select(group => new School()
    {
        SchoolId = group.Key,
        SchoolName = group.First().SchoolName,
        Teachers = data.Where(x => x.SchoolId == group.Key)
            .Select(x => new Teacher()
            {
                TeacherId = x.TeacherId,
                TeacherName = x.TeacherName
            })
            .ToList()
    })
    .ToList();

If you want to share instances of teachers then you can use this

var teachersById = data
    .GroupBy(x => x.TeacherId)
    .Select(group => new Teacher()
    {
        TeacherId = group.Key,
        TeacherName = group.First().TeacherName
    })
    .ToDictionary(x => x.TeacherId);

var schools = data
    .GroupBy(x => x.SchoolId)
    .Select(group => new School()
    {
        SchoolId = group.Key,
        SchoolName = group.First().SchoolName,
        Teachers = teachersById 
            .Where(kv => data
                .Where(x => x.SchoolId == group.Key)
                .Select(x => x.TeacherId)
                .Contains(kv.Key)
            )
            .Select(x => x.Value)
            .ToList()
    })
    .ToList();

Upvotes: 2

usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26924

List<Data> dataList = new List<Data>();

IEnumerable<School> schools = from d in dataList
                              group d by new { SchoolId = d.SchoolId, SchoolName = d.SchoolName } into groupSchool
                              select new School { SchoolId = groupSchool.Key.SchoolId, SchoolName = groupSchool.Key.SchoolName, Teachers =new List<Teacher>(groupSchool.Select(x => new Teacher { TeacherId = x.TeacherId, TeacherName = x.TeacherName })) };

Upvotes: 0

Niyoko
Niyoko

Reputation: 7672

If that is Linq to objects, then use this code

var result = list.GroupBy(x=>new {x.SchoolId, x.SchoolName})
.Select(x=>
{
    var s = new School();
    s.SchoolId = x.Key.SchoolId;
    s.SchoolName = x.Key.SchoolName;
    s.Teachers.AddRange(x.Select(
        y => new Teacher
        {
            TeacherId = y.TeacherId,
            TeacherName = y.TeacherName
        }
    ));

    return s;
});

Please note that above code will result in duplicate Teacher instance even among Teacher with same Id.

Upvotes: 2

Related Questions