2016-12-01 40 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