LINQ中selectManay操作符(五)

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

Sample Code如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    public class Student
    {
        public string Name { get; set; }
        public int Score { get; set; }
        public Student(string name, int score)
        {
            this.Name = name;
            this.Score = score;
        }
    }

    public class Teacher
    {
        public string Name { get; set; }
        public List<Student> Students { get; set; }
        public Teacher(string name, List<Student> students)
        {
            this.Name = name;
            this.Students = students;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //使用集合初始化器初始化Teacher集合
            List<Teacher> teachers = new List<Teacher> { 
                new Teacher(
                    "徐老师",
                    new List<Student>(){
                        new Student("宋江",80),
                        new Student("卢俊义",55),
                        new Student("朱武",45)
                    }
                ),
                new Teacher(
                    "姜老师",
                    new List<Student>(){
                        new Student("林冲",90),
                        new Student("花荣",85),
                        new Student("柴进",58)
                    }
                ),
                new Teacher("樊老师",
                    new List<Student>(){
                        new Student("关胜",100),
                        new Student("阮小七",70),
                        new Student("时迁",30)
                    }
                )
            };

            //问题:查询Score小于60的学生
            //方法1:循环遍历、会有性能的损失
            foreach (Teacher t in teachers)
            {
                foreach (Student s in t.Students)
                {
                    if (s.Score < 60)
                    {
                        Console.WriteLine("姓名:" + s.Name + ",成绩:" + s.Score);
                    }
                }
            }
            Console.WriteLine("-------------------------------------------");

            //方法2:使用SelectMany  延迟加载:在不需要数据的时候,就不执行调用数据,能减轻程序和数据库的交互,可以提供程序的性能,执行循环的时候才去访问数据库取数据
            //只返回学生信息查询表达式
            var query0 = from t in teachers
                         from s in t.Students
                         where s.Score < 60
                         select s;
            foreach (var item in query0)
                Console.WriteLine("姓名:" + item.Name + ",成绩:" + item.Score);
            Console.WriteLine("-------------------------------------------");
            //只返回学生信息查询方法
            var query1 = teachers.SelectMany(t => t.Students).Where(s => s.Score < 60);
            var query2 = teachers.SelectMany(t => t.Students.Where(s => s.Score < 60));
            foreach (var item in query1)
                Console.WriteLine("姓名:" + item.Name + ",成绩:" + item.Score);
            Console.WriteLine("-------------------------------------------");


            //使用匿名类返回教师和学生信息---查询表达式
            var query3 = from t in teachers
                         from s in t.Students
                         where s.Score < 60
                         select new { TeacherName = t.Name, StudentName = s.Name, StudentScore = s.Score };
            foreach (var item in query3)
                Console.WriteLine("教师姓名:" + item.TeacherName + ",学生姓名: " + item.StudentName + ",学生成绩: " + item.StudentScore);
            Console.WriteLine("-------------------------------------------");
            //使用匿名类返回教师和学生信息---查询方法
            var query5 = teachers.SelectMany(t => t.Students.Where(s => s.Score < 60)
                                            .Select(p => new { TeacherName = t.Name, StudentName = p.Name, StudentScore = p.Score }));
            var query6 = teachers.SelectMany(t => t.Students,
                                            (t, s) => new { TeacherName = t.Name, StudentName = s.Name, StudentScore = s.Score })
                                            .Where(n => n.StudentScore < 60);
            foreach (var item in query5)
                Console.WriteLine("教师姓名:" + item.TeacherName + ",学生姓名: " + item.StudentName + ",学生成绩: " + item.StudentScore);
            Console.WriteLine("-------------------------------------------");
            Console.ReadKey();
        }
    }

}
View Code

猜你喜欢

转载自www.cnblogs.com/LuckyZLi/p/12695935.html