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("宋江",),
new Student("卢俊义",),
new Student("朱武",)
}
),
new Teacher("姜老师",
new List<Student>(){
new Student("林冲",),
new Student("花荣",),
new Student("柴进",)
}
),
new Teacher("樊老师",
new List<Student>(){
new Student("关胜",),
new Student("阮小七",),
new Student("时迁",)
}
)
}; //问题:查询Score小于60的学生
//方法1:循环遍历、会有性能的损失
foreach (Teacher t in teachers)
{
foreach (Student s in t.Students)
{
if (s.Score < )
{
Console.WriteLine("姓名:" + s.Name + ",成绩:"+s.Score);
}
}
} //查询表达式
//方法2:使用SelectMany 延迟加载:在不需要数据的时候,就不执行调用数据,能减轻程序和数据库的交互,可以提供程序的性能,执行循环的时候才去访问数据库取数据
//直接返回学生的数据
var query = from t in teachers
from s in t.Students
where s.Score <
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 <
select new {
t,
teacherName=t.Name,
student=t.Students.Where(p=>p.Score<).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 <
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<).ToList());
foreach (var item in query3)
{
Console.WriteLine("姓名:" + item.Name + ",成绩:" + item.Score);
}
Console.ReadKey(); }
}
}
LINQ操作符二:SelectMany的更多相关文章
- LINQ基础(二)
本文主要介绍LINQ查询操作符 LINQ查询为最常用的操作符定义了一个声明语法.还有许多查询操作符可用于Enumerable类. 下面的例子需要用到LINQ基础(一)(http://www.cnblo ...
- 委托发展史(Linq操作符)
嗯~这篇就讲讲Linq吧! 之前讲过Lambda最后进化到了令人发指的地步: Func<string, int> returnLength; returnLength = text =&g ...
- [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询
目录 [LINQ2Dapper]最完整Dapper To Linq框架(一)---基础查询 [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询 [LINQ2Dapp ...
- LINQ 操作符(二)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- linq操作符:分区操作符
Linq中的分区指的是在不重新排列元素的情况下,将输入序列划分为两部分,然后返回其中一个部分的操作. 一.Take操作符 Take(int n)表示将从序列的开头返回数量为n的连续元素,常用于分页.其 ...
- linq操作符:限定操作符
限定操作符运算返回一个Boolean值,该值指示序列中是否有一些元素满足条件或者是否所有元素都满足条件. 一.All操作符 All方法用来确定是否序列中的所有元素都满足条件.看下面的例子: using ...
- linq操作符:元素操作符
元素操作符仅返回一个元素. 一.Fitst操作符 First操作符将返回序列中的第一个元素.如果序列中不包含任何元素,则First<T>方法将引发异常.来看看First()方法的定义: 从 ...
- linq操作符:转换操作符
这些转换操作符将集合转换成数组:IEnumerable.IList.IDictionary等.转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以"As"开头的转换方法可更 ...
- linq操作符:聚合操作符
一.Aggregate操作符 Aggregate操作符对集合值执行自定义聚合运算.来看看Aggregate的定义: public static TSource Aggregate<TSource ...
随机推荐
- Android——Intent动作汇总(转)
String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式.. "android.intent.action.ADD_SHORTCUT" String A ...
- ubuntu16安装配置nginx
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- 理解:Before和:After伪元素
CSS样式表的主要作用是修饰Web页面上的HTML标记,但有时候,为了实现某个效果而往页面里反复添加某个HTML标记很繁琐,或者是显得多余,或者是由于某种原因而做不到.这就是CSS伪元素(Pseudo ...
- Oracle学习笔记之二,Oracle 11g体系结构
Oracle 11g体系结构概述 实例(Instance),是指一组Oracle后台进程以及在服务器中分配的共享内存区域: 数据库(Database),是由基于磁盘的数据文件.控制文件.日志文件.参数 ...
- μCOS-II系统之事件(event)的使用规则及Semaphore实例
**************************************************************************************************** ...
- e到底是什么?
e到底是什么? 今天看到一个下面这么一个简单的求极限问题- 一时恍惚了,为什么结果是e^m. 这个e是什么呢? 然后就百度了下,发现不少文章介绍这个e. 有几篇写得很赞 - An Intuitive ...
- XILINX XST综合的选项的含义
所谓综合,就是将HDL语言.原理图等设计输入翻译成由与.或.非门和RAM.触发器等基本逻辑单元的逻辑连接(网表),并根据目标和要求(约束条件)优化所生成的逻辑连接,生成EDF文件.完成了输入.仿真以及 ...
- 【Android】17.5 利用Messenger实现进程间通信(IPC)
分类:C#.Android.VS2015: 创建日期:2016-03-03 一.Messager类简介 本章前面曾经说过,要在Android上执行带服务的进程间通信(IPC),既可以用Messenge ...
- tree -L n
# tree -L 显示到目录的第二层
- rpm -e 包名 卸载安装的二进制包
rpm -e --nodeps nc-.el6.x86_64 #--nodeps 不包含依赖包,直接删除rpm包