推荐一个开源库,其功能已经完全包含LINQ的所有方法,完全可以替代Linq。而且其有更高的性能和低内存占用的特点。
01 项目简介
ZLinq 是一个由 Cysharp 团队开发的开源项目,目标是为所有 .NET 平台和 Unity 提供零分配的 LINQ 实现。它通过利用 Span 和 SIMD 技术,优化了 LINQ 的性能,同时提供了对树形结构(如文件系统、JSON、游戏对象等)的查询支持。
通过一行代码,调用AsValueEnumerable() 方法,用户可以将任何Linq转换为 ZLinq。
ini
using ZLinq;
var seq = source
.AsValueEnumerable() // 添加此代码
.Where(x => x % 2 == 0)
.Select(x => x * 3);
02 核心特征
零分配 LINQ
传统的 LINQ 实现虽然强大,但在处理大量数据时可能会因为频繁的内存分配而导致性能瓶颈。ZLinq 通过结构体(struct)的方式实现可枚举集合,避免了传统 LINQ 中由于频繁创建对象而导致的内存分配问题。
对 Span 的支持
ZLinq 充分利用了 .NET 9/C# 13 中引入的 allows ref struct 特性,支持对 Span 的操作。这意味着用户可以在支持该特性的环境中,对 Span 类型进行高效的 LINQ 查询操作。
LINQ to SIMD
ZLinq 自动应用 SIMD(单指令多数据)优化,以提升性能。用户可以通过自定义方式进一步优化 SIMD 的使用,以满足特定需求。
LINQ to Tree
ZLinq 扩展了 LINQ 的概念,使其能够应用于树形结构的查询。它提供了类似 LINQ to XML 的轴操作(如 Ancestors、Children、Descendants、BeforeSelf 和 AfterSelf),可以对文件系统、JSON、Unity 的 GameObject 等树形结构进行查询。
03 入门指南
1、AsValueEnumerable(),即可使用 ZLinq 的零分配 LINQ。
ini
using ZLinq;
var source = new int[] { 1, 2, 3, 4, 5 };
// 调用 AsValueEnumerable 以应用 ZLinq
var seq1 = source.AsValueEnumerable().Where(x => x % 2 == 0);
// 也可以应用于 Span(仅在支持 allows ref struct 的 .NET 9/C# 13 环境中)
Span<int> span = stackalloc int[5] { 1, 2, 3, 4, 5 };
var seq2 = span.AsValueEnumerable().Select(x => x * x);
2、LINQ to Tree
LINQ to XML 将围绕轴进行查询的概念引入到了 C# 中。即使你不使用 XML,类似的 API 也被纳入了 Roslyn,并有效地用于探索语法树。ZLinq 扩展了这一概念,使其适用于任何可以被视为树形结构的对象,允许应用 Ancestors、Children、Descendants、BeforeSelf 和 AfterSelf。
具体来说,通过定义一个实现了以下接口的结构体,即可使其可迭代:
csharp
public interface ITraverser<TTraverser, T> : IDisposable
where TTraverser : struct, ITraverser<TTraverser, T> // 自身
{
T Origin { get; }
TTraverser ConvertToTraverser(T next); // 用于 Descendants
bool TryGetHasChild(out bool hasChild); // 可选:优化 Descendants 的使用
bool TryGetChildCount(out int count); // 可选:优化 Children 的使用
bool TryGetParent(out T parent); // 用于 Ancestors
bool TryGetNextChild(out T child); // 用于 Children | Descendants
bool TryGetNextSibling(out T next); // 用于 AfterSelf
bool TryGetPreviousSibling(out T previous); // BeforeSelf
}
3、文件系统
arduino
//安装依赖库
dotnet add package ZLinq.FileSystem
ini
using ZLinq;
var root = new DirectoryInfo("C:\\Program Files (x86)\\Steam");
// FileSystemInfo(FileInfo/DirectoryInfo) 可以调用 `Ancestors`、`Children`、`Descendants`、`BeforeSelf`、`AfterSelf`
var allDlls = root
.Descendants()
.OfType(default(FileInfo)!)
.Where(x => x.Extension == ".dll");
var grouped = allDlls
.GroupBy(x => x.Name)
.Select(x => new { FileName = x.Key, Count = x.Count() })
.OrderByDescending(x => x.Count);
foreach (var item in grouped)
{
Console.WriteLine(item);
}
4、JSON(System.Text.Json)
csharp
//安装依赖库
dotnet add package ZLinq.Json
css
using ZLinq;
// System.Text.Json 的 JsonNode 是 LINQ to JSON 的目标(不是 JsonDocument/JsonElement)。
var json = JsonNode.Parse("""
{
"nesting": {
"level1": {
"description": "First level of nesting",
"value": 100,
"level2": {
"description": "Second level of nesting",
"flags": [true, false, true],
"level3": {
"description": "Third level of nesting",
"coordinates": {
"x": 10.5,
"y": 20.75,
"z": -5.0
},
"level4": {
"description": "Fourth level of nesting",
"metadata": {
"created": "2025-02-15T14:30:00Z",
"modified": null,
"version": 2.1
},
"level5": {
"description": "Fifth level of nesting",
"settings": {
"enabled": true,
"threshold": 0.85,
"options": ["fast", "accurate", "balanced"],
"config": {
"timeout": 30000,
"retries": 3,
"deepSetting": {
"algorithm": "advanced",
"parameters": [1, 1, 2, 3, 5, 8, 13]
}
}
}
}
}
}
}
}
}
}
""");
// JsonNode
var origin = json!["nesting"]!["level1"]!["level2"]!;
// JsonNode axis, Children, Descendants, Anestors, BeforeSelf, AfterSelf and ***Self.
foreach (var item in origin.Descendants().Select(x => x.Node).OfType<JsoArray>())
{
// [true, false, true], ["fast", "accurate", "balanced"], [1, 1, 2, 3, 5, 8, 13]
Console.WriteLine(item.ToJsonString(JsonSerializerOptions.Web));
}
04 项目地址
- End -
推荐阅读
SmartFormat:轻量级文本模板库,轻松替代 string.Format