软件编程
位置:首页>> 软件编程>> C#编程>> LINQ操作符SelectMany的用法

LINQ操作符SelectMany的用法

作者:.NET开发菜鸟  发布时间:2021-08-22 18:20:37 

标签:LINQ,SelectMany

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

示例:

student类:

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

namespace SelectMany操作符
{
   /// <summary>
   /// 学生类
   /// </summary>
   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;
       }
   }
}

teacher类:

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

namespace SelectMany操作符
{
   /// <summary>
   /// Teacher类
   /// </summary>
   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;
       }
   }
}

Program类

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

namespace SelectMany操作符
{
   class Program
   {
       static void Main(string[] args)
       {
           //使用集合初始化器初始化Teacher集合
           List<Teacher> teachers = new List<Teacher> {
              new Teacher("徐老师",
              new List<Student>(){
                new Student("宋江",80),
               new Student("卢俊义",95),
               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);
                   }
               }
           }

//查询表达式
           //方法2:使用SelectMany  延迟加载:在不需要数据的时候,就不执行调用数据,能减轻程序和数据库的交互,可以提供程序的性能,执行循环的时候才去访问数据库取数据
           //直接返回学生的数据
           var query = from t in teachers
                       from s in t.Students
                       where s.Score < 60
                       select s;
           foreach (var item in query)
           {
               Console.WriteLine("姓名:" + item.Name + ",成绩:"+item.Score);
           }
           //只返回老师的数据
           var query1 = from t in teachers
                        from s in t.Students
                        where s.Score < 60
                        select new {
                           t,
                           teacherName=t.Name,
                           student=t.Students.Where(p=>p.Score<60).ToList()
                        };
           foreach (var item in query1)
           {
               Console.WriteLine("老师姓名:" + item.teacherName + ",学生姓名:" +item.student.FirstOrDefault().Name+ ",成绩:" + item.student.FirstOrDefault().Score);
           }
           // 使用匿名类 返回老师和学生的数据
           var query2 = 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 query2)
           {
               Console.WriteLine("老师姓名:" + item.teacherName + ",学生姓名:" + item.studentName + ",成绩:" + item.studentScore);
           }

//使用查询方法
           var query3 = teachers.SelectMany(p => p.Students.Where(t=>t.Score<60).ToList());
           foreach (var item in query3)
           {
               Console.WriteLine("姓名:" + item.Name + ",成绩:" + item.Score);
           }
           Console.ReadKey();

}
   }
}

来源:https://www.cnblogs.com/dotnet261010/p/6849980.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com