Linq基础操作之Select,Where,OrderBy,ThenBy源码分析
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源码分析的更多相关文章
- Linq转换操作之OfType,Cast,AsEnumerable,ToLookup源码分析
Linq转换操作之OfType,Cast,AsEnumerable,ToLookup源码分析 一:Tolookup 1. 从方法的注解上可以看到,ToLookup也是一个k,v的形式,那么问题来了,它 ...
- Linq分组操作之GroupBy,GroupJoin扩展方法源码分析
Linq分组操作之GroupBy,GroupJoin扩展方法源码分析 一. GroupBy 解释: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值. 查询表达式: var ...
- Linq聚合操作之Aggregate,Count,Sum,Distinct源码分析
Linq聚合操作之Aggregate,Count,Sum,Distinct源码分析 一:Linq的聚合运算 1. 常见的聚合运算:Aggregate,Count, Sum, Distinct,Max, ...
- Linq生成操作之DefautIfEmpty,Empty,Range,Repeat源码分析
Linq生成操作之DefautIfEmpty,Empty,Range,Repeat源码分析 Linq的四种生成运算 DefautIfEmpty,Empty,Range,Repeat 也就是给我们初始化 ...
- Linq特取操作之ElementAt,Single,Last,First源码分析
Linq特取操作之ElementAt,Single,Last,First源码分析 一:linq的特取操作 First/FirstOrDefault, Last/LastOrDefault, Eleme ...
- Linq扩展最后遗留之SelectMany,Zip,SequenceEqual源码分析
Linq扩展最后遗留之SelectMany,Zip,SequenceEqual源码分析 一: AsParallel [并行化查询] 这个函数的功效就是将计算结果多线程化.[并行计算] =>[多核 ...
- Java基础系列--07_Object类的学习及源码分析
Object: 超类 (1)Object是类层次结构的顶层类,是所有类的根类,超类. 所有的类都直接或者间接的继承自Object类. 所有对象(包括数组)都实现这个类的方法 (2)Object ...
- 第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+ ...
- Linq集合操作之Intersect,Except,Union源码分析
Linq集合操作之Intersect,Except,Union源码分析 linq的集合运算 常见的集合运算有哪些? 这三个扩展方法在我们实际使用中用的还是非常多的,而且这里还涉及到了“复杂度” 无算法 ...
随机推荐
- curl_setopt设置发送cookie信息
1.php curl访问会话传递问题 curl_setopt($ch , CURLOPT_COOKIE , 'PHPSESSID=A7281E0926CB37D791AD464CDD646CF2') ...
- 一行命令解决 xcode升级新版本插件失效问题
sudo find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth ...
- Linux Centos 7 RabbitMQ 安装
下载地址:http://www.rabbitmq.com/releases/rabbitmq-server/ 找到rabbitmq-server-3.6.15-1.el7.noarch.rpm 第一步 ...
- 黑暗之光 Day2
1. 鼠标点击UI检测 UICamera.isOverUI 2. 鼠标指针管理 public class CussorManager : MonoBehaviour { public static C ...
- 修改SecureCRT终端的Home和End功能键。
SecureCRT真是个不错的ssh客户端工具,但在使用时发现跟自己的一些使用习惯不符合,例如home.end.pageup.pagedown和delete等键. 默认情况下一些按键的功能如下: pa ...
- jquery排序与动态添加option以及属性
function getOrgansid() { url="<%=basePath%>/rest/bsc/organ/selectOrganSidAllList"; $ ...
- shell编程——流控制case和select
在shell编程里有时候需要出现交换界面,让使用者来选择要执行的功能,如下面所示,这时候就需要用到case和select进行配合 请选择功能: 1) 退出 2) 系统升级 3) 防火墙配置 4) to ...
- Using Mono DLLs in a Unity Project
[Using Mono DLLs in a Unity Project] The path to the Unity DLLs will typically be: 一个生成dll的例子如下: mcs ...
- Spring Data JPA动态查询(多条件and)
entity: @Entity @Table(name = "data_illustration") public class Test { @Id @GenericGenerat ...
- Web内容回顾
-----------------siwuxie095 Java EE 三层结构 1.Web 层:Struts2 框架 2.Service 层:Spring 框架 3.DAO 层:Hibernate ...