接下来我们对思维导图中的方法进行简单讲解
1.1 Join
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.join?view=net-9.0
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.zip?view=net-9.0
1.3 GroupJoin
GroupJoin方法源代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.groupjoin?view=net-9.0
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.groupjoin?view=net-9.0
ToList方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/ToCollection.cs#L51C13-L61C46
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist?view=net-9.0
定义:基于指定的类型过滤IEnumerable
OfType方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Cast.cs#L13C13-L18C52
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.oftype?view=net-9.0
2.3 ToArray
定义:从IEnumerable<T>创建一个Array
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.toarray?view=net-9.0
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.toarray?view=net-9.0
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.todictionary?view=net-9.0
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.cast?view=net-9.0
定义: 将序列的每个元素投影为新形式。
Select方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Select.cs#L71C13-L86C53
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=net-9.0
3.2 SelectMany
定义:将序列的每个元素投影到IEnumerable<T>并将结果序列平展为一个序列。
SelectMany方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/SelectMany.cs#L112C13-L132C83
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.selectmany?view=net-9.0
4. Aggregation
定义:尝试确定序列中的元素数量而不强制枚举。
TryGetNonEnumeratedCount方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Count.cs#L97C13-L125C26
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.trygetnonenumeratedcount?view=net-9.0
4.2 Average
Average方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Average.cs#L76C72-L76C117
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.average?view=net-9.0
4.3 Aggregate(By)
定义:对序列应用累加器函数。指定的种子值用作累加器的初始值,而指定的函数用于选择结果值。
Average方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Aggregate.cs#L62C13-L83C43
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate?view=net-9.0
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregateby?view=net-9.0
4.4 Max(By)
定义:返回序列中的最大值
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.max?view=net-9.0
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.maxby?view=net-9.0
定义:返回序列中的最小值
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.min?view=net-9.0
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.minby?view=net-9.0
4.6 LongCount
定义:返回序列中元素的数量,返回的是Int64类型
LongCount方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Count.cs#L130C13-L147C26
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.longcount?view=net-9.0
4.7 Sum
定义:计算序列中数字值的总和
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sum?view=net-9.0
5. Set
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.union?view=net-9.0
5.2 Intersect(By)
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersect?view=net-9.0
IntersectBy方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Intersect.cs#L60C13-L73C78
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersectby?view=net-9.0
5.3 Except(By)
定义:计算两个序列的差集
Except方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Except.cs#L12C13-L22C56
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=net-9.0
ExceptBy方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Except.cs#L49C163-L49C205
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.exceptby?view=net-9.0
6. Quantification
6.1 Any
定义:判断序列中是否存在元素
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any?view=net-9.0
6.2 All
定义:确定序列中的所有元素是否都满足指定的条件。
All方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/AnyAll.cs#L48C13-L67C10
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.all?view=net-9.0
定义:确定序列是否包含一个指定的元素
Contains方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Contains.cs#L11C13-L12C42
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.contains?view=net-9.0
定义:比较两个序列中元素的相等性,这里比较引用的相等性
SequenceEqual方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/SequenceEqual.cs#L11C13-L11C47
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-9.0
7. Squence
定义:生成一个序列包含相同的值
Repeat方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Repeat.cs#L13C13-L23C64
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.repeat?view=net-9.0
定义:在指定范围内生成一个整数的序列
Range方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Range.cs#L13C13-L24C52
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.range?view=net-9.0
Prepend方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs#L25C13-L32C90
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.prepend?view=net-9.0
定义:反转序列中的元素
Reverse方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Reverse.cs#L13C13-L23C57
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.reverse?view=net-9.0
定义:在序列的末尾添加一个值
Append方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs#L13C13-L20C89
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.append?view=net-9.0
7.6 Empty
定义:返回一个具有指定类型参数的空IEnumerable
Empty方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Enumerable.cs#L16C13-L16C35
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.empty?view=net-9.0
Chunk方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Chunk.cs#L38C13-L53C48
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.chunk?view=net-9.0
定义:对序列中的元素进行升序或降序排列
Order方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/OrderBy.cs#L46C13-L48C76
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.order?view=net-9.0
OrderDescending方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/OrderBy.cs#L72C13-L72C52
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderdescending?view=net-9.0
定义:根据指定的条件对序列中的元素进行升序或降序排列
OrderBy方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/OrderBy.cs#L51C16-L51C92
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderby?view=net-9.0
OrderByDescending方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/OrderBy.cs#L96C13-L96C88
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderbydescending?view=net-9.0
8.3 ThenBy
ThenBy方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/OrderBy.cs#L103C13-L108C77
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.thenby?view=net-9.0
定义:返回一个 IEnumerable<T> 的元素,如果序列为空,则返回一个具有默认值的单元素集合。
DefaultIfEmpty方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs#L12C13-L12C44
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.defaultifempty?view=net-9.0
定义:返回序列的最后一个元素
Last方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Last.cs#L13C13-L19C26
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.last?view=net-9.0
LastOrDefault方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Last.cs#L34C13-L34C37
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.lastordefault?view=net-9.0
定义:返回序列中指定索引位置的元素
ElementAt方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/ElementAt.cs#L14C13-L37C28
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.elementat?view=net-9.0
ElementAtOrDefault方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/ElementAt.cs#L109C13-L125C28
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.elementatordefault?view=net-9.0
9.4 Single(OrDefault)
定义:返回序列中单个元素
Single方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Single.cs#L23C13-L29C28
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.single?view=net-9.0
SingleOrDefault方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Single.cs#L33C16-L33C42
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.singleordefault?view=net-9.0
定义:返回序列中的第一个元素
First方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/First.cs#L13C13-L19C27
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.first?view=net-9.0
FirstOrDefault方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/First.cs#L60C13-L61C50
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=net-9.0
定义:对序列中的元素进行分组
GroupBy方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Grouping.cs#L92C13-L92C90
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.groupby?view=net-9.0
10.2 CountBy
定义:根据指定的条件对集合中的元素进行分组并根据分组计算数量
CountBy方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/CountBy.cs#L13C13-L27C70
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.groupby?view=net-9.0
定义:从元素的结尾跳过指定数量的元素并将剩余的元素返回
SkipLast方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Skip.cs#L131C13-L141C63
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.skiplast?view=net-9.0
定义:返回一个新的序列,包含了最后N个元素的集合
TakeLast方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Take.cs#L264C13-L273C59
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.takelast?view=net-9.0
定义:根据指定条件过滤序列中的元素
Where方法源代码:hhttps://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Where.cs#L14C13-L44C76
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.where?view=net-9.0
定义:从序列的开始位置返回指定数量的连续元素。
Take方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Take.cs#L13C13-L20C45
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.take?view=net-9.0
TakeWhile方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Take.cs#L225C13-L240C57
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.takewhile?view=net-9.0
定义:跳过序列中指定数量的元素,返回剩余的元素
Skip方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Skip.cs#L12C13-L38C48
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.skip?view=net-9.0
SkipWhile方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Skip.cs#L43C13-L58C57
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.skipwhile?view=net-9.0
定义:返回序列中不同的元素
Distinct方法源代码:https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Linq/src/System/Linq/Distinct.cs#L15C13-L25C68
示例代码:
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=net-9.0
参考文章:https://www.linkedin.com/feed/update/urn:li:activity:6971346130747977728/
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable?view=net-9.0