将<object>列表到层次结构

时间:2016-10-19 07:47:42

标签: c# linq hierarchy hierarchical-data

我有List<Data>数据

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

名单是平的:

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

所以基本上老师可以属于很多学校。

如何将上面的平面列表转换为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; }
} 

4 个答案:

答案 0 :(得分:2)

如果这是Linq to objects,那么使用此代码

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;
});

请注意,上述代码即使在具有相同Teacher的{​​{1}}中也会产生重复的Teacher个实例。

答案 1 :(得分:2)

这种方法的顺序是O(n ^ 2),它不共享教师实例。

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();

如果您想分享教师的实例,那么您可以使用此

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();

答案 2 :(得分:0)

Capabilities

答案 3 :(得分:0)

item.SchoolIdNote:它是伪代码(对于简短的代码,我使用带有参数的构造函数而不是使用属性)

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。实际上你使用了错误的数据库表示(你应该把学校的老师分成两个不同的表)。