Linq加入两个列表

时间:2016-04-22 11:39:46

标签: c# linq

我是Linq的新手。我有一个要求,有2个列表,一个是学生列表,另一个是标记列表。在学生中我们有id,姓名和标记列表id(sid),标记,科目。需要输出每个学生的学生ID,姓名和总分。我已经接近了以下代码,

 class Student
 {
     public int Id { get; set; }
     public string name { get; set; }
 }

 class Marks
 {
     public int Sid {get;set;}
     public int mark {get;set;}
     public string subjects {get;set;}
 }

 class Program
 {
     static void Main(string[] args)
     {
         //student
        List<Student> studentList = new List<Student>()
        {
             new Student() { Id = 1, name = "A" },
             new Student() { Id = 2, name = "B" }
        };

        //marks
        List<Marks> markList = new List<Marks>()
        {
            new Marks() { Sid = 1, mark = 30, subjects = "A1" },
            new Marks() { Sid = 1, mark = 40, subjects = "A2" },
            new Marks() { Sid = 2, mark = 40, subjects = "B1" },
            new Marks() { Sid = 1, mark = 50, subjects = "B2" }
        };

         var result=(from stu in studentList
                     join mar in markList 
                         on stu.Id equals mar.Sid                            
                     select new
                         {
                            stu.Id,
                            stu.name,
                            mar.mark,
                            mar.subjects
                         })
                     .GroupBy(x => x.subjects).ToList();

以上Linq查询并不符合我的要求。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagReportViewIds"
    android:canRetrieveWindowContent="true"
    android:notificationTimeout="100" />

MarksCount - 此学生的总分数 标记 - 该学生的分数列表