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

一:Tolookup

1. 从方法的注解上可以看到,ToLookup也是一个k,v的形式,那么问题来了,它和Dcitionary有什么区别???

可以看到ToDictionray中的源码是这样的:

foreach (TSource current in source)
{
dictionary.Add(keySelector(current), elementSelector(current));
}

大家有没有看到,这个源码有什么不合实际的地方。。。

如果key重复了,那么这个ToDictionary却对是要报错的。。。。

现在我的要求来了,如果说key重复了,我希望value中的值类型起来。这个就是我们今天要讲的ToLookup要解决的问题。

二:lookup的源码分析

Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
foreach (TSource current in source)
{
lookup.GetGrouping(keySelector(current), true).Add(elementSelector(current));
}

大家课后可以仔细看一看。

三:OfType转换运算符 (筛选制定类型)

1. 解释: 根据指定类型筛选 System.Collections.IEnumerable 的元素。

     var list = new List<object>() { , "3D",  };

     var array = list.OfType<int>(); //结果是 int 集合

通过demo,我们可以看到OfType就是筛选指定的类型。

很可惜,ILSpy看不到。

四:Cast运算符

1. 解释: 将 System.Collections.IEnumerable 的元素强制转换为指定的类型。

这也告诉我们,ILSpy也不是所有的代码都能看得到。。。

Cast的扩展方法

1. 将 IEnumerable 的元素强制转换为指定的类型。 这句话有强大的误导性

System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("mango");
fruits.Add("apple");
fruits.Add("lemon");

IEnumerable<string> query =
fruits.Cast<string>().OrderBy(fruit => fruit).Select(fruit => fruit);

为什么这个可以cast成功的原因是什么???

string => object => string[cast]

这个过程叫做拆装箱。。。

static void Main(string[] args)
{
var list = new int[] { 10, 20, 30 };

var query = list.Cast<object>().ToList(); //装箱

var myquery = query.Cast<int>().ToList(); //拆箱

var myquery2 = list.Cast<long>().ToList(); //隐式转换 [不可以]
}

可以看得到,Cast并不是我们看到如解释一样那么容易理解。

五:asEnumerable

如果当前的类型没有继承IEumverable这个接口,那么我们可以强制将这个类型转换为继承子

IEnumverable接口的类。

var table = new DataTable();

table.AsEnumerable().Select(i => i.Field<string>(""));

Linq转换操作之OfType,Cast,AsEnumerable,ToLookup源码分析的更多相关文章

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

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

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

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

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

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

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

    Linq基础操作之Select,Where,OrderBy,ThenBy源码分析 二:Select 它是延迟执行.yield有得一拼,因为他们都是生成了一个枚举类. if (source is TSo ...

  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. Linq转换操作之ToArray,ToList,ToDictionary源码分析

    Linq转换操作之ToArray,ToList,ToDictionary源码分析 一:linq中的转换运算符 1. ToArray 我们经常用在linq查询上吧. linq只能运用在IEnumerab ...

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

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

  9. Linq限定操作之All,Any,Contains源码分析

    Linq限定操作之All,Any,Contains源码分析 linq的限定操作 常见的限定操作: All,Any,Contains 一:All 1. 解释: 确定序列中的所有元素是否满足条件. 从字面 ...

随机推荐

  1. 21_java之File对象和递归遍历

    01IO技术概述 * A:IO技术概述 * a: Output * 把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作 * b: Input * 把持久设备上的数据读取到内存中的这 ...

  2. [Z]Password-based encryption in Java: salt and key derivation

    http://www.javamex.com/tutorials/cryptography/pbe_salt.shtml 另外,这个网站好像有很多很深入而且很详尽的教程.目测是个类似于官方的或者大牛们 ...

  3. leetcode671

    class Solution { public: vector<int> V; void postTree(TreeNode* node) { if (node != NULL) { V. ...

  4. Eclipse weblogic 中文乱码问题解决办法

  5. win 10+ iis 10 部署.net core 1.1 web api

    今天上午部署了wcf,部署了好久,一直没有部署好,最后找到了dudu的部署方法,结果中午吃饭的时候成功了,这是链接:http://www.cnblogs.com/dudu/p/3328066.html ...

  6. Coursera连接不上(视频无法播放),修改hosts文件

    视频问题 如果Coursera网站连接不上,或者视频加载不出来.可以通过如下方式进行配置:   一.找到hosts文件 Windows 系统, hosts文件位于: [C:\Windows\Syste ...

  7. go_常量与枚举

    package main import ( "fmt" "math" ) //常量的数值可以作为各种类型使用 func consts(){ const file ...

  8. 【hdu4135】【hdu2841】【hdu1695】一类通过容斥定理求区间互质的方法

    [HDU4135]Co-prime 题意 给出三个整数N,A,B.问在区间[A,B]内,与N互质的数的个数.其中N<=10^9,A,B<=10^15. 分析 容斥定理的模板题.可以通过容斥 ...

  9. Spark 性能相关参数配置详解-任务调度篇

    随着Spark的逐渐成熟完善, 越来越多的可配置参数被添加到Spark中来, 本文试图通过阐述这其中部分参数的工作原理和配置思路, 和大家一起探讨一下如何根据实际场合对Spark进行配置优化. 由于篇 ...

  10. ARGB_8888

    原文出处:http://www.cnblogs.com/and_he/archive/2012/12/22/argb.html A:透明度 R:红色 G:绿 B:蓝 Bitmap.Config ARG ...