LINQ操作符SelectMany的用法
作者:.NET开发菜鸟 时间:2021-08-22 18:20:37
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
标签:LINQ,SelectMany
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringSecurity实现访问控制url匹配
2021-11-03 00:18:16
![](https://img.aspxhome.com/file/2023/1/63401_0s.jpg)
mybatis-plus 如何使用雪花算法ID生成策略
2023-04-06 07:15:38
![](https://img.aspxhome.com/file/2023/7/106087_0s.jpg)
Java后台批量生产echarts图表并保存图片
2023-11-25 03:24:39
利用maven引入第三方jar包以及打包
2023-11-15 04:23:17
![](https://img.aspxhome.com/file/2023/0/59010_0s.jpg)
详解Spring循环依赖的解决方案
2022-05-29 13:14:57
盘点几种常见的java排序算法
2023-09-17 10:13:10
![](https://img.aspxhome.com/file/2023/0/78260_0s.png)
android调试工具DDMS的使用详解
2023-06-21 09:06:22
![](https://img.aspxhome.com/file/2023/4/82414_0s.png)
AQS加锁机制Synchronized相似点详解
2023-08-04 22:36:55
![](https://img.aspxhome.com/file/2023/8/57918_0s.png)
springboot结合maven配置不同环境的profile方式
2022-05-28 12:00:16
![](https://img.aspxhome.com/file/2023/6/63416_0s.png)
一文告诉你为什么要重写hashCode()方法和equals()方法
2021-09-05 07:05:55
Java实现几种常见排序算法代码
2022-10-10 20:42:19
Java Main 函数启动不退出的解决方案
2022-03-24 14:25:03
Android编程单选项框RadioGroup综合应用示例
2022-01-12 19:16:29
![](https://img.aspxhome.com/file/2023/4/87294_0s.jpg)
java 解析由String类型拼接的XML文件方法
2022-07-10 12:47:14
C#多线程学习之Thread、ThreadPool、Task、Parallel四者区别
2023-08-27 05:32:14
![](https://img.aspxhome.com/file/2023/2/108772_0s.png)
C#复杂XML反序列化为实体对象两种方式小结
2022-08-05 16:50:45
![](https://img.aspxhome.com/file/2023/4/82214_0s.png)
C#基于COM方式读取Excel表格的方法
2021-09-15 12:35:36
C# 开发日志本地化工具
2023-08-27 21:57:05
![](https://img.aspxhome.com/file/2023/0/108670_0s.png)
java 进制转换实例详解
2023-07-05 11:53:45
Java日常练习题,每天进步一点点(41)
2023-05-18 18:10:02