2016-12-01 32 views
3

这是我从数据库来的原始数据:从C#端集团通过原始数据从数据库

PrimaryColumn StudentId StudentName CourseName CourseId CourseDuration 
    1    1   X   Cse1  C1   2 
    2    1   X   Cse2  C2   1 
    3    1   X   Cse3  C3   3 
    4    2   Y   Cse1  C1   2 
    5    2   Y   Cse4  C4   5 

类:

public class Student 
{ 
    public int StudentId {get; set;} 
    public string StudentName {get; set} 
    public List<Course> Courses {get; set;} 
}  

public class Course 
{ 
    public int CourseId {get; set;} 
    public string CourseName {get; set;} 
    public int CourseDuration {get; set; } 
} 

我的目标是获取由学生和课程分组数据他们将使用List<Course>作为学生类的属性。

所以,我认为目标是相当前进的。所以,我继续使用GroupBy来处理从DB到C#的原始数据,希望得到结果但无济于事。

到目前为止,这是我得到的最好的。

masterData.GroupBy(x => new { x.StudentId, x.StudentName }, (key, group) => new { StudentId = key.StudentId, StudentName = key.StudentName, Courses=group.ToList() }).ToList(); 

尽管这并没有让我得到我想要的东西。通过一些次要的代码解决方法发布此调用,我能够实现我所需要的。但是,每当我无法正确分组原始数据时,我都会感到厌烦。

如果任何人都能向我展示正确的方式来进一步优化上面的代码或者完全不同的方式,这将是非常有帮助的。

这是我需要的结果是在一个List<Student>:

Student: 
    StudentId: 1 
    StudentName: X 
    List<Course> Courses: [0] - { CourseId: C1, CourseName = Cse1, CourseDuration = 2} 
          [1] - { CourseId: C2, CourseName = Cse2, CourseDuration = 1} 
          [2] - { CourseId: C3, CourseName = Cse3, CourseDuration = 3} 
Student: 
    StudentId: 2 
    StudentName: Y 
    List<Course> Courses: [0] - { CourseId: C1, CourseName = Cse1, CourseDuration = 2} 
          [1] - { CourseId: C4, CourseName = Cse4, CourseDuration = 5} 

干杯

+0

请正确编写您的类声明。它不是公立班级学生,是公立班级学生。 – mybirthname

+0

这不是你的问题的答案,但你的表结构是有问题的。当课程持续时间发生变化时会发生什么?更新表中的所有值?更好的办法是有单独的学生,课程和StudentCourses表,请参阅[数据库规范化](https://en.wikipedia.org/wiki/Database_normalization) – HebeleHododo

+0

@ HebeleHododo我同意,但这是我得到的数据,而不是数据库中存在的,我无法控制的。 – portatoriacqua

回答

1

你可以不喜欢这样的方式:

这里完整的例子:dotNetFiddle

List<Student> result = data.GroupBy(x => new { x.StudentID, x.StrudentName }, 
      (key, group) => new Student{ 
              StudentId = key.StudentID, 
              StudentName = key.StrudentName,     
              Courses = GetCourses(group)}).ToList(); 

    //You can do this operation with Reflection if you want. If you don't want to write manually the property names. 
    public static List<Course> GetCourses(IEnumerable<RawData> data) 
    { 
     List<Course> course = new List<Course>(); 

     foreach(var item in data) 
     { 
      Course c = new Course(); 

      c.CourseDuration = item.CourseDuration; 
      c.CourseId = item.CourseID; 
      c.CourseName = item.CourseName; 

      course.Add(c); 
     } 

     return course; 
    } 
+1

做到了!谢谢 – portatoriacqua

0

我会做你的情况(但我不确定你会看到它作为'正确分组')是沿着

var groupedStudents = masterData.GroupBy(x => x.StudentId); 
foreach (var studentGroup in groupedStudents) 
{ 
    // new studentclass 
    var student = new Student(studentGroup.First().StudentId, studentGroup.First().StudentName); 
    foreach (var studentRecord in studentGroup) 
    { 
     student.Courses.Add(new course(studentRecord.CourseId, studentRecord.CourseName, studentRecord.CourseDuration); 
    } 

    // add the student-object to where you want to save them i.e. 
    this.MyStudentList.Add(student); 
} 
+0

我的解决方法与此类似。谢谢您的好意。 – portatoriacqua

0

这里的问题是,数据库是组织不良,保持远离正常状态。但是,你可以处理好它是否正确地分离到不同的表格。你提取课程,学生再这样,他们汇总 - 下面的代码应该给一个线索

// get all courses 
var courses = masterData.GroupBy(x => x.CourseId).Select(group => group.First()) 
.Select(x => new Course {CourseId = x.CourseId, CourseName = x.CourseName, ...}) 
.ToDictionary(x => x.CourseId); 

// get all students (with empty courses for now) 
var students = masterData.GroupBy(x => x.StudentId).Select(group => group.First()) 
.Select(x => new Student {StudentId = x.StudentId, ...}) 
.ToDictionary(x => x.StudentId); 

// fill students with courses 
foreach(var data in masterData) 
{ 
    student[data.StudentId].Courses.Add(courses[data.CourseId]) 
} 

我想这是可能后表正常化重复使用一条明路。或者,您可以尝试编写一个复杂的LINQ,通过单个查询来完成所有这些工作人员。

+0

哈哈。我同意,但是,DB不是我控制的那个。感谢您的回应。 – portatoriacqua