網站首頁 編程語言 正文
SelectMany操作符提供了將多個from子句組合起來的功能,相當于數據庫中的多表連接查詢,它將每個對象的結果合并成單個序列。
示例:
student類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SelectMany操作符 { ////// 學生類 /// 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操作符 { ////// Teacher類 /// public class Teacher { //姓名 public string Name { get; set; } //學生集合 public ListStudents { get; set; } public Teacher(string name, List 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集合 Listteachers = new List { new Teacher("徐老師", new List (){ new Student("宋江",80), new Student("盧俊義",95), new Student("朱武",45) } ), new Teacher("姜老師", new List (){ new Student("林沖",90), new Student("花榮",85), new Student("柴進",58) } ), new Teacher("樊老師", new List (){ 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
相關推薦
- 2021-12-17 element-ui dialog彈窗 點擊空白處使彈窗區不關閉
- 2022-11-17 Python?pyecharts模塊安裝與入門教程_python
- 2022-11-09 golang包循環引用的幾種解決方案總結_Golang
- 2022-04-14 ASP.NET?Core基礎之Main方法講解_基礎應用
- 2023-05-23 Django?事務回滾的具體實現_python
- 2023-07-02 oracle數據庫排序后如何獲取第一條數據_oracle
- 2022-06-24 使用Python獲取字典鍵對應值的兩種方法_python
- 2022-07-26 論網頁檢查preview和response一個是漢字一個是亂碼怎么解決
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支