保举一个开源库,其功能已经完全包罗LINQ的全部方法,完全可以替换Linq。而且其有更高的性能和低内存占用的特点。
01 项目简介
ZLinq 是一个由 Cysharp 团队开辟的开源项目,目标是为全部 .NET 平台和 Unity 提供零分配的 LINQ 实现。它通过利用 Span 和 SIMD 技能,优化了 LINQ 的性能,同时提供了对树形结构(如文件体系、JSON、游戏对象等)的查询支持。
通过一行代码,调用AsValueEnumerable() 方法,用户可以将任何Linq转换为 ZLinq。
- 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。
- 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。
详细来说,通过界说一个实现了以下接口的结构体,即可使其可迭代:
- 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、文件体系
- //安装依赖库
- dotnet add package ZLinq.FileSystem
复制代码- 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)
- //安装依赖库
- dotnet add package ZLinq.Json
复制代码- 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 项目所在
https://github.com/Cysharp/ZLinq
- End -
保举阅读
2个零根本入门框架教程!
SmartFormat:轻量级文本模板库,轻松替换 string.Format
Squidex:一个基于.Net功能强盛的CMS开源项目
barcodelib:一个功能强盛且易于利用的 C# 条形码天生库
一文把握DeepSeek本地摆设+Page Assist欣赏器插件+C#接口调用+局域网访问!全攻略来了!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|