电竞比分网-中国电竞赛事及体育赛事平台

分享

LINQ操作符二:SelectMany

 張小龍net館藏 2018-01-31

SelectMany操作符提供了將多個(gè)from子句組合起來的功能,相當(dāng)于數(shù)據(jù)庫中的多表連接查詢,它將每個(gè)對(duì)象的結(jié)果合并成單個(gè)序列。

示例:

student類:

復(fù)制代碼
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SelectMany操作符
 8 {
 9     /// <summary>
10     /// 學(xué)生類
11     /// </summary>
12     public class Student
13     {
14         //姓名
15         public string Name { get; set; }
16         //成績(jī)
17         public int Score { get; set; }
18         //構(gòu)造函數(shù)
19         public Student(string name, int score)
20         {
21             this.Name = name;
22             this.Score = score;
23         }
24     }
25 }
復(fù)制代碼

teacher類:

復(fù)制代碼
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SelectMany操作符
 8 {
 9     /// <summary>
10     /// Teacher類
11     /// </summary>
12     public class Teacher
13     {
14         //姓名
15         public string Name { get; set; }
16         //學(xué)生集合
17         public List<Student> Students { get; set; }
18 
19         public Teacher(string name, List<Student> students)
20         {
21             this.Name = name;
22             this.Students = students;
23         }
24     }
25 }
復(fù)制代碼

Program類

復(fù)制代碼
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SelectMany操作符
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //使用集合初始化器初始化Teacher集合
14             List<Teacher> teachers = new List<Teacher> { 
15                new Teacher("徐老師",
16                new List<Student>(){
17                  new Student("宋江",80),
18                 new Student("盧俊義",95),
19                 new Student("朱武",45)
20                }
21                ),
22                 new Teacher("姜老師",
23                new List<Student>(){
24                  new Student("林沖",90),
25                 new Student("花榮",85),
26                 new Student("柴進(jìn)",58)
27                }
28                ),
29                 new Teacher("樊老師",
30                new List<Student>(){
31                  new Student("關(guān)勝",100),
32                 new Student("阮小七",70),
33                 new Student("時(shí)遷",30)
34                }
35                )
36             };
37 
38             //問題:查詢Score小于60的學(xué)生
39             //方法1:循環(huán)遍歷、會(huì)有性能的損失
40             foreach (Teacher t in teachers)
41             {
42                 foreach (Student s in t.Students)
43                 {
44                     if (s.Score < 60)
45                     {
46                         Console.WriteLine("姓名:" + s.Name + ",成績(jī):"+s.Score);
47                     }
48                 }
49             }
50 
51             //查詢表達(dá)式
52             //方法2:使用SelectMany  延遲加載:在不需要數(shù)據(jù)的時(shí)候,就不執(zhí)行調(diào)用數(shù)據(jù),能減輕程序和數(shù)據(jù)庫的交互,可以提供程序的性能,執(zhí)行循環(huán)的時(shí)候才去訪問數(shù)據(jù)庫取數(shù)據(jù)            
53             //直接返回學(xué)生的數(shù)據(jù)
54             var query = from t in teachers
55                         from s in t.Students
56                         where s.Score < 60
57                         select s;
58             foreach (var item in query)
59             {
60                 Console.WriteLine("姓名:" + item.Name + ",成績(jī):"+item.Score);
61             }
62             //只返回老師的數(shù)據(jù)
63             var query1 = from t in teachers
64                          from s in t.Students
65                          where s.Score < 60
66                          select new { 
67                             t,
68                             teacherName=t.Name,
69                             student=t.Students.Where(p=>p.Score<60).ToList()
70                          };
71             foreach (var item in query1)
72             {
73                 Console.WriteLine("老師姓名:" + item.teacherName + ",學(xué)生姓名:" +item.student.FirstOrDefault().Name+ ",成績(jī):" + item.student.FirstOrDefault().Score);
74             }
75             // 使用匿名類 返回老師和學(xué)生的數(shù)據(jù)
76             var query2 = from t in teachers
77                          from s in t.Students
78                          where s.Score < 60
79                          select new { teacherName=t.Name, studentName=s.Name,studentScore=s.Score };           
80             foreach (var item in query2)
81             {
82                 Console.WriteLine("老師姓名:" + item.teacherName + ",學(xué)生姓名:" + item.studentName + ",成績(jī):" + item.studentScore);
83             }
84 
85             //使用查詢方法
86             var query3 = teachers.SelectMany(p => p.Students.Where(t=>t.Score<60).ToList());
87             foreach (var item in query3)
88             {
89                 Console.WriteLine("姓名:" + item.Name + ",成績(jī):" + item.Score);
90             }
91             Console.ReadKey();
92 
93         }
94     }
95 }
復(fù)制代碼

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多