.NET 9 Preview 1 中 Linq 更新
Intro
.NET 9 Preview 1 中在之前的 .NET 版本的基础上又新增了一些 Linq 方法的支持
Index
.NET 9 里引入了一个
Index
的 Linq 方法,我们可以在 foreach 的时候获取到对应的 Index
类似于使用
Select((index, item) => )
,更简单一些,示例如下:
var employees = Enumerable.Range(1, 10)
.Select(x => new
{
Id = x,
Level = x % 4,
Name = $"xxx {x}",
Score = x * 10
})
.ToArray();
// Index
Console.WriteLine("Index sample:");
foreach (var (index, item) in employees.Index())
{
Console.WriteLine($"Index: {index}, Id: {item.Id}, {JsonSerializer.Serialize(item)}");
}
输出结果如下:
CountBy
在 .NET 6 中引入了一系列的 XxxBy 系列的 Linq 方法如:
MaxBy
/
MinBy
/
DistinctBy
...
在 .NET 9 里也引入了
CountBy
和
AggregateBy
的扩展
先来看下
CountBy
的示例,还是使用之前的数据:
Console.WriteLine("CountBy sample:");
foreach (var (key, count) in employees.CountBy(x => x.Level))
{
Console.WriteLine($"Level {key}, Count: {count}");
}
这里我们按照 Level 做一个 Count,相当于按 Level 做一个 GroupBy,输出结果如下:
AggregateBy
AggregateBy
使用示例如下:
Console.WriteLine("AggregateBy sample:");
foreach (var (key, total) in employees.AggregateBy(s => s.Level, 0, (a, x) => a + x.Score))
{
Console.WriteLine($"Level {key}: Total: {total}");
}
Console.WriteLine();
foreach (var (key, total) in employees.AggregateBy(s => s.Level, x => x * 10, (a, x) => a + x.Score))
{
Console.WriteLine($"Level {key}: Total: {total}");
}
Aggregate
可以按照有一个 seed,第一个示例中 seed 是 0,第二个示例根据 aggregate 的字段来设置,输出结果如下:
第一个例子相当于按 level 做了一个 groupBy 之后,group 里按 score 取了一个 Sum
第二个例子在第一个例子的基础之上,修改了 seed,相当于在 Sum 的基础上又加上了
level * 10
References
https://github.com/dotnet/runtime/issues/95563
https://github.com/dotnet/runtime/pull/95947
https://github.com/dotnet/runtime/issues/77716
https://github.com/dotnet/runtime/pull/91507
https://github.com/dotnet/runtime/issues/91533
https://github.com/dotnet/runtime/pull/92089
https://github.com/WeihanLi/SamplesInPractice/blob/main/net9sample/Net9Samples/LinqSample.cs