Linq基础操作之Select,Where,OrderBy,ThenBy源码分析

二:Select

它是延迟执行。yield有得一拼,因为他们都是生成了一个枚举类。

if (source is TSource[])
{
return new Enumerable.WhereSelectArrayIterator<TSource, TResult>((TSource[])source, null, selector);
}

可以清楚的看到WhereSelectArrayIterator<TSource, TResult> 是一个枚举类。

WhereSelectArrayIterator<TSource, TResult> => numerable.Iterator<TResult> => IEnumerable<TSource>

大家应该清楚,延迟执行的本质是什么??? 枚举类。

public override bool MoveNext()
{
if (this.state == 1)
{
while (this.index < this.source.Length)
{
TSource arg = this.source[this.index];
this.index++;
if (this.predicate == null || this.predicate(arg))
{
this.current = this.selector(arg);
return true;
}
}
this.Dispose();
}
return false;
}

所以说,大家一定要对foreach这个语法糖有一个清楚的认识。

可以看到,foreach遍历数组的时候,用到了内部的一个ArrayEnumerator枚举类。

三:Where

我们知道where应该是用于筛选操作。

var list = new int[] { 10, 20, 30 };

var query = list.Where(i => i / 20 == 0).ToList();

然后我们来分析一下代码:

我们看到,其实where方法也是用到了内部的一个WhereArrayIterator<TSource> 枚举类,同时我们也看到了一个奇葩的
公共父类Enumerable.Iterator<TSource>,对吧,当我们知道枚举类的时候,你应该重点去查看MoveNext这个方法。

public override bool MoveNext()
{
if (this.state == 1)
{
while (this.index < this.source.Length)
{
TSource tSource = this.source[this.index];
this.index++;
if (this.predicate(tSource))
{
this.current = tSource;
return true;
}
}
this.Dispose();
}
return false;
}

通过这个MoveNext,我们应该非常清楚这个Where的业务逻辑。

四:OrderBy,ThenBy源码分析

var list = new int[] { 10, 20, 30 };

var query = list.OrderByDescending(i => i).ToList();

可以看到OrderBy返回的是一个new OrderedEnumerable<TSource, TKey> 的一个类。

当你从OrderedEnumerable类型上面调用ToList,也就执行了GetEnumerator方法。

也就是说这个方法才是我们排序的关键。

我们发现所谓的orderby方法,其实最后调用的是 EnumerableSorter<TElement>.Sort方法。。

而这个Sort用到了“快速排序”。

《2》ThenBy就是在OrderBy的基础上进行了第二轮排序。

如果大家接触过sql server的话,应该明白二次排序。

一种类似嵌套的方式来做的。

id name age

3 jack 22
1 john 32
2 mary 20

Linq基础操作之Select,Where,OrderBy,ThenBy源码分析的更多相关文章

  1. Linq转换操作之OfType,Cast,AsEnumerable,ToLookup源码分析

    Linq转换操作之OfType,Cast,AsEnumerable,ToLookup源码分析 一:Tolookup 1. 从方法的注解上可以看到,ToLookup也是一个k,v的形式,那么问题来了,它 ...

  2. Linq分组操作之GroupBy,GroupJoin扩展方法源码分析

    Linq分组操作之GroupBy,GroupJoin扩展方法源码分析 一. GroupBy 解释: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值. 查询表达式: var ...

  3. Linq聚合操作之Aggregate,Count,Sum,Distinct源码分析

    Linq聚合操作之Aggregate,Count,Sum,Distinct源码分析 一:Linq的聚合运算 1. 常见的聚合运算:Aggregate,Count, Sum, Distinct,Max, ...

  4. Linq生成操作之DefautIfEmpty,Empty,Range,Repeat源码分析

    Linq生成操作之DefautIfEmpty,Empty,Range,Repeat源码分析 Linq的四种生成运算 DefautIfEmpty,Empty,Range,Repeat 也就是给我们初始化 ...

  5. Linq特取操作之ElementAt,Single,Last,First源码分析

    Linq特取操作之ElementAt,Single,Last,First源码分析 一:linq的特取操作 First/FirstOrDefault, Last/LastOrDefault, Eleme ...

  6. Linq扩展最后遗留之SelectMany,Zip,SequenceEqual源码分析

    Linq扩展最后遗留之SelectMany,Zip,SequenceEqual源码分析 一: AsParallel [并行化查询] 这个函数的功效就是将计算结果多线程化.[并行计算] =>[多核 ...

  7. Java基础系列--07_Object类的学习及源码分析

    Object: 超类 (1)Object是类层次结构的顶层类,是所有类的根类,超类.   所有的类都直接或者间接的继承自Object类.   所有对象(包括数组)都实现这个类的方法 (2)Object ...

  8. 第1节 Scala基础语法:scala中的方法源码分析

    val list=List(1,2,3,4) list.reduce((x:Int,y:Int)=>x+y)--->list.reduceLeft((x:Int,y:Int)=>x+ ...

  9. Linq集合操作之Intersect,Except,Union源码分析

    Linq集合操作之Intersect,Except,Union源码分析 linq的集合运算 常见的集合运算有哪些? 这三个扩展方法在我们实际使用中用的还是非常多的,而且这里还涉及到了“复杂度” 无算法 ...

随机推荐

  1. 使用COM口的2、3针的通断作为中端源(有一个读图像的摄像头,当把卡插到位时触发中端,防止在插卡的过程中出现不稳定的图像)

    利用串口2读,串口3发数据的特点.建立不断的发送流,再从接收端接收.如果收到,则数据畅通,否则断开.相当于产生一个中断.这样电脑对外部事件可作出反应. using System;using Syste ...

  2. prettytable模块(格式化打印内容)

    1.查看系统是否已经安装prettytable模块 2.下载prettytable模块 登陆:https://pypi.python.org/pypi/PrettyTable 3.安装PrettyTa ...

  3. RocketMQ入门(简介、特点)

    简介: RocketMQ作为一款纯java.分布式.队列模型的开源消息中间件,支持事务消息.顺序消息.批量消息.定时消息.消息回溯等. 发展历程: 1. Metaq(Metamorphosis) 1. ...

  4. python 协程 gevent 简单测试

    串行测试 from gevent import monkey; monkey.patch_all()#有IO才做时需要这一句 import gevent import requests,time st ...

  5. js添加对象数组

    json 数组也是数组  var jsonstr="[{'name':'a','value':1},{'name':'b','value':2}]"; var jsonarray  ...

  6. 在springBoot在控制台打印sql语句

    在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增: logging.level.com.dy.springboot.server.mapper= ...

  7. [iOS]在xcode的iOS虚拟机中对BLE(蓝牙4.0)进行调试

    本文档的主要内容可参考苹果官方资料: http://developer.apple.com/library/ios/#technotes/tn2295/_index.html 1. 买一个CSR蓝牙4 ...

  8. 【原】Coursera—Andrew Ng机器学习—Week 9 习题—异常检测

    [1]异常检测 [2]高斯分布 [3]高斯分布 [4] 异常检测 [5]特征选择 [6] [7]多变量高斯分布 Answer: ACD B 错误.需要矩阵Σ可逆,则要求m>n  测验1 Answ ...

  9. schedule与scheduleAtFixedRate之Timer源码分析

    执行Timer任务调度方法有如下几种: 这些方法最后调用的都是这个方法: private void sched(TimerTask task, long time, long period)   这个 ...

  10. NoSuchBeanDefinitionException:No qualifying bean of type found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

    报错如下: NoSuchBeanDefinitionException:No qualifying bean of type   found for dependency: expected at l ...