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的更多相关文章

  1. LINQ基础(二)

    本文主要介绍LINQ查询操作符 LINQ查询为最常用的操作符定义了一个声明语法.还有许多查询操作符可用于Enumerable类. 下面的例子需要用到LINQ基础(一)(http://www.cnblo ...

  2. 委托发展史(Linq操作符)

    嗯~这篇就讲讲Linq吧! 之前讲过Lambda最后进化到了令人发指的地步: Func<string, int> returnLength; returnLength = text =&g ...

  3. [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询

    目录 [LINQ2Dapper]最完整Dapper To Linq框架(一)---基础查询 [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询 [LINQ2Dapp ...

  4. LINQ 操作符(二)

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

  5. linq操作符:分区操作符

    Linq中的分区指的是在不重新排列元素的情况下,将输入序列划分为两部分,然后返回其中一个部分的操作. 一.Take操作符 Take(int n)表示将从序列的开头返回数量为n的连续元素,常用于分页.其 ...

  6. linq操作符:限定操作符

    限定操作符运算返回一个Boolean值,该值指示序列中是否有一些元素满足条件或者是否所有元素都满足条件. 一.All操作符 All方法用来确定是否序列中的所有元素都满足条件.看下面的例子: using ...

  7. linq操作符:元素操作符

    元素操作符仅返回一个元素. 一.Fitst操作符 First操作符将返回序列中的第一个元素.如果序列中不包含任何元素,则First<T>方法将引发异常.来看看First()方法的定义: 从 ...

  8. linq操作符:转换操作符

    这些转换操作符将集合转换成数组:IEnumerable.IList.IDictionary等.转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以"As"开头的转换方法可更 ...

  9. linq操作符:聚合操作符

    一.Aggregate操作符 Aggregate操作符对集合值执行自定义聚合运算.来看看Aggregate的定义: public static TSource Aggregate<TSource ...

随机推荐

  1. 阿里云ECS服务器Linux环境下配置php服务器(二)--phpMyAdmin篇

    上一篇讲了PHP服务器的基本配置,我们安装了apache,php,还有MySQL,最后还跑通了一个非常简单的php页面,有兴趣的朋友可以看我的这篇博客: 阿里云ECS服务器Linux环境下配置php服 ...

  2. Java虚拟机学习 - 对象内存分配与回收 ( 5 )

    对象优先在Eden上分配 大多数情况下,对象优先在新生代Eden区域中分配.当Eden内存区域没有足够的空间进行分配时,虚拟机将触发一次 Minor GC(新生代GC).Minor GC期间虚拟机将E ...

  3. Android学习系列(2)--App自动更新之通知栏下载

    这篇文章是Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用.1.设计思路,使用VersionCode定义为版本升级参数. android为我们定义版本提供了2个属性:& ...

  4. 百度UEditor富文本编辑器去除过滤div等标签

    将设计排版好的页面html代码上传到数据库,再读取出来的时候发现所有的div都被替换成了p标签. 解决方法: 首先在ueditor.all.js文件内搜索allowDivTransToP,找到如下的代 ...

  5. jenkins 批量修改配置文件

    jenkins 批量修改配置文件   jenkin job 修改配置 修改前配置 <runPostStepsIfResult> <name>FAILURE</name&g ...

  6. spine 2.1.27 Pro 叠加方式(Blending)

    将spine更新到2.1.27 Pro,发现有更多的叠加方式可用了,如图: 以前则只有Normal和Additive可选. 更多的叠加方式对于用spine做特效动画还是比较有用的.不过我还没试这些叠加 ...

  7. OpenVpn的ipp.txt文件

    ipp=ip pool我猜得,呵呵 ipp.txt文件中存放上一次连接时,客户端分配的ip地址. ipp.txt用来保存上次的连接状态的,并不能为客户端设置固定ip地址. 该文件的格式为:用户名,ip ...

  8. jQuery判断复选框是否勾选

    一个功能复选框勾选时给input表单赋值,复选框取消时将表单值清除. 功能:复选框勾选时给input表单赋值,复选框取消时将表单值清除. 实现源码:cyfID为复选框的id $("#cyfI ...

  9. 同一个界面内取微信的OPENID和调用微信的分享接口

    步骤如下,1:判断URL是否有CODE参数传入,没有则拼接那个微信跳转连接,然后redirect2:有CODE传入,调用微信接口,根据code获取openid和access_token,注意这一步取到 ...

  10. [sh]shell案例

    调用同目录下的ip.txt内容: 路径 [root@lanny ~]# pwd /root txt文件 [root@lanny ~]# cat ip.txt 10.1.1.1 10.1.1.2 10. ...