zoukankan      html  css  js  c++  java
  • LINQ操作符二:SelectMany

    SelectMany操作符提供了将多个from子句组合起来的功能,相当于数据库中的多表连接查询,它将每个对象的结果合并成单个序列。

    示例:

    student类:

     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     /// 学生类
    11     /// </summary>
    12     public class Student
    13     {
    14         //姓名
    15         public string Name { get; set; }
    16         //成绩
    17         public int Score { get; set; }
    18         //构造函数
    19         public Student(string name, int score)
    20         {
    21             this.Name = name;
    22             this.Score = score;
    23         }
    24     }
    25 }

    teacher类:

     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         //学生集合
    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 }

    Program类

     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("柴进",58)
    27                }
    28                ),
    29                 new Teacher("樊老师",
    30                new List<Student>(){
    31                  new Student("关胜",100),
    32                 new Student("阮小七",70),
    33                 new Student("时迁",30)
    34                }
    35                )
    36             };
    37 
    38             //问题:查询Score小于60的学生
    39             //方法1:循环遍历、会有性能的损失
    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 + ",成绩:"+s.Score);
    47                     }
    48                 }
    49             }
    50 
    51             //查询表达式
    52             //方法2:使用SelectMany  延迟加载:在不需要数据的时候,就不执行调用数据,能减轻程序和数据库的交互,可以提供程序的性能,执行循环的时候才去访问数据库取数据            
    53             //直接返回学生的数据
    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 + ",成绩:"+item.Score);
    61             }
    62             //只返回老师的数据
    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 + ",学生姓名:" +item.student.FirstOrDefault().Name+ ",成绩:" + item.student.FirstOrDefault().Score);
    74             }
    75             // 使用匿名类 返回老师和学生的数据
    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 + ",学生姓名:" + item.studentName + ",成绩:" + 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 + ",成绩:" + item.Score);
    90             }
    91             Console.ReadKey();
    92 
    93         }
    94     }
    95 }
  • 相关阅读:
    PHP 实现简易 IOC 容器
    MySQL 5.7原生JSON格式支持
    PHP 使用 array_map 替代 foreach
    深入了解PHP闭包的使用以及实现
    PHP补全固定数字位数前面有0的值
    面试技巧
    Flex 布局语法教程
    如何在phpexcel中设置自动高度(自动换行)?
    宝塔面板和wdcp有什么区别?哪个比较好用?
    linux下如何查找nginx配置文件的位置
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/6849980.html
Copyright © 2011-2022 走看看