zoukankan      html  css  js  c++  java
  • Linq转换操作之OfType,Cast,AsEnumerable,ToLookup源码分析

    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>() { 20, "3D", 24 };
    
         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>(""));

  • 相关阅读:
    UVa 12174 (滑动窗口) Shuffle
    UVa 1607 (二分) Gates
    CodeForces ZeptoLab Code Rush 2015
    HDU 1525 (博弈) Euclid's Game
    HDU 2147 (博弈) kiki's game
    UVa 11093 Just Finish it up
    UVa 10954 (Huffman 优先队列) Add All
    CodeForces Round #298 Div.2
    UVa 12627 (递归 计数 找规律) Erratic Expansion
    UVa 714 (二分) Copying Books
  • 原文地址:https://www.cnblogs.com/dragon-L/p/6444657.html
Copyright © 2011-2022 走看看