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. VLOOKUP函数常用套路大全

    今天和大家来说说VLOOKUP的那些事儿,深入了解一下VLOOKUP函数的各种用法,看看这位大众情人还藏着多少不为人知的秘密.函数的语法为:VLOOKUP(要找谁,在哪儿找,返回第几列的内容,精确找还 ...

  2. 学习ASP.NET Core Razor 编程系列二——添加一个实体

    在Razor页面应用程序中添加一个实体 在本篇文章中,学习添加用于管理数据库中的书籍的实体类.通过实体框架(EF Core)使用这些类来处理数据库.EF Core是一个对象关系映射(ORM)框架,它简 ...

  3. 简述Java三大特性

    1.面向对象有三大特性,分别是:封装.继承和多态.2.封装:面向对象的封装就是把描述一个对象的属性和行为的代码封装在一个类中,有些属性是不希望公开的,或者说被其他对象访问的,所以我们使用private ...

  4. 【Python】 Web开发框架的基本概念与开发的准备工作

    Web框架基本概念 现在再来写这篇文章显然有些马后炮的意思.不过正是因为已经学习了Flask框架, 并且未来计划学习更加体系化的Django框架,在学习过程中碰到的很多术语等等,非常有必要通过这样一篇 ...

  5. Jmeter 前置处理器 BeanShell_PreProcessor 适用思考

    首先摘抄一段官方文档的话: Before invoking the script, some variables are set up in the BeanShell interpreter: lo ...

  6. android 滑动分页

    import android.app.ListActivity;import android.os.Bundle;import android.os.Handler;import android.vi ...

  7. java基础笔记(8)----接口

    接口 是特殊的抽象类,纯抽象类---所有方法都是抽象方法 接口和抽象类的区别: 相同点: 编译后,会分别生成对应的.class文件 都不能创建对象(实例化),但是可以生成引用(使用多态) 不同点: 抽 ...

  8. JavaScript(第二十天)【DOM操作表格及样式】

    DOM在操作生成HTML上,还是比较简明的.不过,由于浏览器总是存在兼容和陷阱,导致最终的操作就不是那么简单方便了.本章主要了解一下DOM操作表格和样式的一些知识. 一.操作表格 <table& ...

  9. 用C语言协助办公_01 找出所有不对劲的人

    近期想出一系列用C语言协助办公的视频教程,这是第一个.具体的移步:https://chuanke.baidu.com/v6658388-240377-1789288.html

  10. PTA的使用简介

    PTA(Programming Teaching Assistant)是PAT(Programming Ability Test)的配套练习平台. 1.关于PAT PAT(Programming Ab ...