Linq 虽然用得多,但是里面有一些方法比较少用,因此整理一下。Enumerable 类的所有方法可以在 MSDN 上查阅到:https://msdn.microsoft.com/zh-cn/library/system.linq.enumerable.aspx

Aggregate

这个方法有三个重载,先看第一个

Aggregate<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>)

参数是接受两个 TSource 类型的输入,返回一个 TSource 类型的输出。

按照 MSDN 上的说明,输入的第一个参数是累加的值,第二个参数是元素。

{
int[] array = new[] { , , , , };
int result = array.Aggregate((sum, i) => sum + i);
Console.WriteLine(result);
}
{
string[] array = new string[] { "hello", "world", "!" };
string result = array.Aggregate((combine, str) =>
{
return combine + " " + str;
});
Console.WriteLine(result);
}

则会输出 15 和 hello world !

在第一次进入 Aggregate 的 Func 时,combine 的值为第一个元素的值,str 为第二个元素的值。

当输入的序列的元素个数为 0 时,则抛出 InvalidOperationException 异常。

而当元素的个数只有一个的时候,则不会执行 Func。

接下来看第二个重载

Aggregate<TSource, TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>)

比起第一个重载,多了一个参数输入参数 TAccumulate,泛型参数也变成了两个。

{
int[] array = new[] { , , , , };
long result = array.Aggregate(-1L, (sum, i) =>
{
return sum + i;
});
Console.WriteLine(result);
}

那么这段代码的输出则会是 14。第一次进入 Func 的时候,sum 的值为 -1L,i 的值是 1,这个行为跟第一个重载稍微有点区别。

而且当元素只有一个的时候,也是会进入 Func 的

当序列为空的时候,也不会触发到异常

直接等于输入参数的值。

第三个重载

Aggregate<TSource, TAccumulate, TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)

这个其实就是相当与第二个重载增加了最后一个参数

{
string[] array = new string[] { "hello", "world", "!" };
string result = array.Aggregate("start", (combine, str) =>
{
return combine + " " + str;
}, end => end.ToUpperInvariant());
Console.WriteLine(result);
}

执行后会输出 START HELLO WORLD !。

最后的那个参数相对于对最终结果进行了一下处理,跟下面的代码是等价的。

{
string[] array = new string[] { "hello", "world", "!" };
string result = array.Aggregate("start", (combine, str) =>
{
return combine + " " + str;
}).ToUpperInvariant();
Console.WriteLine(result);
}

DefaultIfEmpty

第一个重载

DefaultIfEmpty<TSource>(IEnumerable<TSource>)

就是说,如果一个序列的元素个数是零个的话,那就返回一个只有一个 default(TSource) 元素的序列。感觉这没啥用(lll¬ω¬)

看另一个重载

DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)

多了个参数,猜也猜得出来了

唔,好像还是没啥实用意义…………

Except

Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)

求差集

A 序列减去 B 序列,并且去重了。

另一重载

Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

多了一个比较器

最后只会输出 Hello,因为在 IgnoreCase 比较器下,world 和 WORLD 是一样的。

GroupBy

虽然用得还是比较多,但是重载比较多,还是写一下吧。

GroupBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)

这个是最简单的重载了。

根据 Age 分组,这个重载很简单,也是最常用的。

GroupBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>)

多了一个比较器,不难

Key 会根据第一次匹配到的值。

GroupBy<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>)

第一个重载的改版而且,如果将上面的 person => person.Name 改为 person => person,那跟第一个重载没区别。

GroupBy<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)

多了一个比较器而已,不说了。

GroupBy<TSource, TKey, TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey, IEnumerable<TSource>, TResult>)

分完组后对每一组进行了一下处理。

GroupBy<TSource, TKey, TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey, IEnumerable<TSource>, TResult>, IEqualityComparer<TKey>)

比上面多了一个比较器,不说了。

GroupBy<TSource, TKey, TElement, TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, Func<TKey, IEnumerable<TElement>, TResult>)

多了元素选择的参数重载,参考上面。

GroupBy<TSource, TKey, TElement, TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, Func<TKey, IEnumerable<TElement>, TResult>, IEqualityComparer<TKey>)

多了选择器,不说。

Join

Join<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)

这个其实不难,只要参考一下 SQL 中的 inner join 的话。

先初始化测试数据

List<Person> list1 = new List<Person>()
{
new Person()
{
Id = ,
Gender = "M"
},
new Person()
{
Id = ,
Gender = "F"
},
new Person()
{
Id = ,
Gender = "M"
}
};
List<Student> list2 = new List<Student>()
{
new Student()
{
Id = ,
Name = "martin"
},
new Student()
{
Id = ,
Name = "valid void"
},
new Student()
{
Id = ,
Name = "justin"
}
};

然后测试代码走起

没啥难的,等价于以下的 linq 写法

Join<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<TKey>)

多了个比较器,用于比较 key,不说了。

GroupJoin

GroupJoin<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)

看上去很复杂,但其实可以参考 Join 的输入进行对比。

测试数据我们还是沿用 Join 的。执行测试代码

对等的 linq 写法如下

var result = (from person in list1
join student in list2 on person.Id equals student.Id into studentGroup
select new
{
Id = person.Id,
Gender = person.Gender,
Students = studentGroup.ToList()
}).ToList();

GroupJoin<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>, IEqualityComparer<TKey>)

多了一个比较器,不说了。

SelectMany

这个最近这段时间用得比较多,也记录一下吧

SelectMany<TSource, TResult>(IEnumerable<TSource>, Func<TSource, IEnumerable<TResult>>)

测试代码

简单的来说就是将一个 IEnumerable<IEnumerable<T>> 的序列变成一个 IEnumerable<T> 的序列。

对等的 linq 写法

感觉 linq 写法会相对比较好理解的说。

SelectMany<TSource, TResult>(IEnumerable<TSource>, Func<TSource, Int32, IEnumerable<TResult>>)

Func 多了个 Int32 的参数,看测试代码

很好理解,就是当前的索引。

SelectMany<TSource, TCollection, TResult>(IEnumerable<TSource>, Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)

比起第一个就是多了个结果执行而已

会进入这个 Func 四次,前两次是 helloword 那个 List,后两次是 justin martin 那个 List。

SelectMany<TSource, TCollection, TResult>(IEnumerable<TSource>, Func<TSource, Int32, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)

多了索引,参考上面。

SequenceEqual

序列比较,知道有这个东西,但平时好像没有怎么用过。

SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)

很好理解,首要前提肯定是元素个数相等,其次要每一个元素相等。

SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

多了个比较器,不说了。

SkipWhile

Skip 倒是一直在用,SkipWhile 就用得比较少,也记录一下吧。

SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)

不难,就是当 Func 返回 false 时停止。

因为当去到 3 的时候为 false,因此返回 3 和剩下的元素。

SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource, Int32, Boolean>)

多了个索引而已,没啥好说的。

ToDictionary

先看第一个重载

ToDictionary<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)

不难,Func 就是获取按照什么来生成 Key。

另外使用这个方法是要注意,Key 是不能重复的。

所以说实话,这方法平时比较少用。。。

ToDictionary<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>)

多了一个比较器,没啥好说的。

ToDictionary<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>)

比起第一个重载,多了一个如何生成字典的值的 Func,也没啥好说的。

ToDictionary<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)

多了比较器,不说了。

ToLookup

这个有点像上面的 ToDictionary 的。

ToLookup<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)

跟 ToDictionary 的第一个重载的输入参数是一样的。

ILookup<int, Person> 这个的结构类似于一个数组,然后每个数组的元素是一个 Group。

当元素的 Key 重复的时候:

那么这个 lookup 就只有一个 group 了,但这个 group 就会有多个元素。

ToLookup<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>)

ToLookup<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>)

ToLookup<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)

这三个重载可以参考一下 ToDictionary 的重载,一样的说。

Zip

这个方法就只有一个,没别的重载

Zip<TFirst, TSecond, TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst, TSecond, TResult>)

这里就借用 MSDN 上的示例代码了,看结果也看得出 Zip 这个操作的逻辑了。遍历两个序列进行操作,直到其中一个到达尾部。

另外像 TakeWhile 可以参考上面的 SkipWhile 就不说了。Distinct、Union 和 Intersect 平时也用得比较多,因此也不说了。

整理一下 System.Linq.Enumerable 类中的那些比较少用的方法的更多相关文章

  1. System.ExecutionEngineException: Attempting to JIT compile method System.Linq.Enumerable

    关于JIT编译和AOT编译的问题.IOS下是不支持JIT动态编译的,所以如果程序有涉及JIT编译的都会无法执行. 在google查过说unity是不支持部分的Linq功能,如Sort方法. 但我在un ...

  2. 编写一个ComputerAverage抽象类,类中有一个抽象方法求平均分average,可以有参数。定义 Gymnastics 类和 School 类,它们都是 ComputerAverage 的子类。Gymnastics 类中计算选手的平均成绩的方法是去掉一个最低分,去掉一个最高分,然后求平均分;School 中计算平均分的方法是所有科目的分数之和除以总科目数。 要求:定义ComputerAv

    题目: 编写一个ComputerAverage抽象类,类中有一个抽象方法求平均分average,可以有参数. 定义 Gymnastics 类和 School 类,它们都是 ComputerAverag ...

  3. Object类中wait代餐方法和notifyAll方法和线程间通信

    Object类中wait代餐方法和notifyAll方法 package com.yang.Test.ThreadStudy; import lombok.SneakyThrows; /** * 进入 ...

  4. 使用Visual Studio进行单元测试-Shim类中无法找到参数包含CancellationTokenSource的方法

    Shim类中无法找到参数包含CancellationTokenSource的方法,这句话有点绕口,看例子. 一.代码 public class CancellationDemo { public in ...

  5. 在Action类中获得HttpServletResponse对象的四种方法

    在struts1.xAction类的execute方法中,有四个参数,其中两个就是response和request.而在Struts2中,并没有任何参数,因此,就不能简单地从execute方法获得Ht ...

  6. 关于在C#中对类中的隐藏基类方法和重写方法的理解

    最近在学习C#,在C#中的类看到重写和隐藏基类的方法这些概念.才开始感觉自己不是很理解这些概念.也区分不开这些概念.通过自己的查找资料和练习后.慢慢的理解了类中的隐藏和重写这个概念.在C#中只有在基类 ...

  7. 谈谈Runtime类中的freeMemory,totalMemory,maxMemory几个方法

    最近在网上看到一些人讨论到java.lang.Runtime类中的freeMemory(),totalMemory(),maxMemory ()这几个方法的一些问题,很多人感到很疑惑,为什么,在jav ...

  8. 简述 Python 类中的 __init__、__new__、__call__ 方法

    任何事物都有一个从创建,被使用,再到消亡的过程,在程序语言面向对象编程模型中,对象也有相似的命运:创建.初始化.使用.垃圾回收,不同的阶段由不同的方法(角色)负责执行. 定义一个类时,大家用得最多的就 ...

  9. Appium java-client库更新到6.x ,TouchAction类中弃用的函数及替代方法

    新版本的java-client已经取消swipe方法,很多TouchAction类中的很多老方法也都已经弃用,具体可以参考这边的官方说明文档: https://static.javadoc.io/io ...

随机推荐

  1. BZOJ4825 单旋

    分析:一道水题,去年考场发现了特点但是不会splay维护挂了,然后现在我写了个treap. 画一画图就可以解决这道题了,自己试一下. 代码如下: #include<bits/stdc++.h&g ...

  2. R语言学习 第八篇:常用的数据处理函数

    Basic包是R语言预装的开发包,包含了常用的数据处理函数,可以对数据进行简单地清理和转换,也可以在使用其他转换函数之前,对数据进行预处理,必须熟练掌握常用的数据处理函数,本文分享在数据处理时,经常使 ...

  3. 一个类似抖音 APP 拍摄按钮效果的控件

    TouchButton 一个类似抖音 APP 拍摄按钮效果的控件 效果图预览 用法 <net.angrycode.library.TouchButton android:id="@+i ...

  4. Linux编辑器篇-分享10个最好的Markdown编辑器

    在这篇文章中,兄弟连Linux培训会分享一些可以在 Linux 上安装使用的最好的 Markdown 编辑器.虽然你在 Linux 平台上能找到非常多的 的 Markdown 编辑器,但是在这里我们将 ...

  5. JAVAEE——BOS物流项目08:配置代理对象远程调用crm服务、查看定区中包含的分区、查看定区关联的客户

    1 学习计划 1.定区关联客户 n 完善CRM服务中的客户查询方法 n 在BOS项目中配置代理对象远程调用crm服务 n 调整定区关联客户页面 n 实现定区关联客户 2.查看定区中包含的分区 n 页面 ...

  6. nginx+lua 根据指定路径反向代理

    location /imgproxytest{ if ($uri ~ ".*\.(jpg|png|jpeg|bmp|gif|swf|css)$"){ rewrite_by_lua ...

  7. 【译】Java、Kotlin、RN、Flutter 开发出来的 App 大小,你了解过吗?

    现在开发 App 的方式非常多,原生.ReactNative.Flutter 都是不错的选择.那你有没有关注过,使用不同的方式,编译生成的 Apk ,大小是否会有什么影响呢?本文就以一个最简单的 He ...

  8. 利用whoosh对mongoDB的中文文档建立全文检索

    1.建立索引 #coding=utf-8 from __future__ import unicode_literals __author__ = 'zh' import sys,os from wh ...

  9. 《UNIX网络编程 卷1:套接字联网API》读书笔记(一):网络编程简介

    概述 要编写通过计算机网络通信的程序,首先要确定这些程序相互通信所用的协议.大多数网络是按照划分成客户和服务器来组织的.本章及后续章节的焦点是TCP/IP协议族,也可称为网际协议族.下图为客户与服务器 ...

  10. 访问限制:由于对必需的库 C:/Program Files/Java/jre6/lib/rt.jar 具有一定限制,因此无法访问类型。。

    在项目上单击右键选择 属性 Java编译器 错误或警告 选择启用特定于项目的设置 建议不要使用和限制使用的API将 禁止的引用(访问规则) 设置为 警告 然后应用即可解决