簡體   English   中英

如何使用LINQ(C#)連接兩個集合並將集合作為一個實體轉置

[英]How to join two collection and transpose a collection as an entity using LINQ (c#)

我有以下課程:

public class Student
{
    public string StudentID { get; set; }
    public string StudentName { get; set; }
}
public class Marks
{
    public string StudentID { get; set; }
    public string SubjectName { get; set; }
    public string Score { get; set; }
} 

填充集合:

Collection<Student> objStudentCollection = new Collection<Student>();
Student student = new Student();
student.StudentID = "104517";
student.StudentName = "John";
objStudentCollection.Add(student);

student = new Student();
student.StudentID = "104520";
student.StudentName = "Stella";
objStudentCollection.Add(student);

Collection<Marks> objMarkCollection = new Collection<Marks>();
Marks marks = new Marks();
marks.StudentID = "104517";
marks.SubjectName = "English";
marks.Score = "85";
objMarkCollection.Add(marks);

marks = new Marks();
marks.StudentID = "104517";
marks.SubjectName = "Science";
marks.Score = "60";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104517";
marks.SubjectName = "Mathematics";
marks.Score = "75";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104517";
marks.SubjectName = "Optional 1";
marks.Score = "75";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104520";
marks.SubjectName = "French";
marks.Score = "54";
objMarkCollection.Add(marks);

marks = new Marks();
marks.StudentID = "104520";
marks.SubjectName = "Science";
marks.Score = "60";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104520";
marks.SubjectName = "Mathematics";
marks.Score = "75";
objMarkCollection.Add(marks);


marks = new Marks();
marks.StudentID = "104520";
marks.SubjectName = "Optional 1";
marks.Score = "50";
objMarkCollection.Add(marks);

我想將以上集合綁定在GridView中顯示為:

學生證| 學生姓名| 英文| 法語| 數學| 科學| 可選1 |

我認為您需要利用GroupJoin方法來獲得您想要的東西。 如果主題列表固定且不會更改,則可以這樣進行:

 
 
 
  
  var q = objStudentCollection .GroupJoin( objMarkCollection, stu => stu.StudentID, mark => mark.StudentID, (stu, mark) => new { student.StudentID, student.StudentName, English = mark.Where(m => m.SubjectName == "English").Sum(m => Convert.ToInt32(m.Score)), French = mark.Where(m => m.SubjectName == "French").Sum(m => Convert.ToInt32(m.Score)), Mathematics = mark.Where(m => m.SubjectName == "Mathematics").Sum(m => Convert.ToInt32(m.Score)), Science = mark.Where(m => m.SubjectName == "Science").Sum(m => Convert.ToInt32(m.Score)), Optional1 = mark.Where(m => m.SubjectName == "Optional 1").Sum(m => Convert.ToInt32(m.Score)), Total = mark.Sum(m => Convert.ToInt32(m.Score)), })
 
  

它不是世界上最漂亮的代碼,但是它將為您提供一個匿名類型,每一行都包含您要的數據。 如果每個主題只有一個分數,則可以用 SingleOrDefault代替 Sum

 var subjects = objMarkCollection .Select(mark => mark.SubjectName) .Distinct() .Dump(); var q = objStudentCollection .GroupJoin( objMarkCollection, stu => stu.StudentID, mark => mark.StudentID, (stu, mark) => new { student.StudentID, student.StudentName, Marks = from s in subjects join m in mark on s equals m.SubjectName into outer from o in outer.DefaultIfEmpty() select new { SubjectName = s, Score = (o == null) ? 0 : Convert.ToInt32(o.Score), }, Total = mark.Sum(m => Convert.ToInt32(m.Score)), }) .Dump(); 

第二種解決方案將為您創建一個帶有每個學生的匿名類型,並為包括每個主題的分數集(該學生未參加的分數為0)打分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM