{"id":27215358,"url":"https://github.com/sallushan/linq-variable-finder","last_synced_at":"2025-07-11T10:06:22.442Z","repository":{"id":286455757,"uuid":"961456371","full_name":"sallushan/linq-variable-finder","owner":"sallushan","description":"This project is a case study for an algorithm that extracts the captured variable values from an Expression Tree.","archived":false,"fork":false,"pushed_at":"2025-04-06T15:08:14.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T06:57:55.203Z","etag":null,"topics":["algorithm","algorithms-and-data-structures","csharp","expression","linq","optimization","orm"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sallushan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-04-06T15:06:22.000Z","updated_at":"2025-04-06T15:12:12.000Z","dependencies_parsed_at":"2025-04-10T04:15:24.605Z","dependency_job_id":null,"html_url":"https://github.com/sallushan/linq-variable-finder","commit_stats":null,"previous_names":["sallushan/linq-variable-finder"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sallushan/linq-variable-finder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sallushan%2Flinq-variable-finder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sallushan%2Flinq-variable-finder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sallushan%2Flinq-variable-finder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sallushan%2Flinq-variable-finder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sallushan","download_url":"https://codeload.github.com/sallushan/linq-variable-finder/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sallushan%2Flinq-variable-finder/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264780932,"owners_count":23662749,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","algorithms-and-data-structures","csharp","expression","linq","optimization","orm"],"created_at":"2025-04-10T04:15:20.681Z","updated_at":"2025-07-11T10:06:22.435Z","avatar_url":"https://github.com/sallushan.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Exploring Expression Tree Variable Extraction in .NET\n\nThis repository is a **practical study** on how to extract captured variables from LINQ expression trees in .NET — comparing traditional visitor-based traversal vs. a precomputed traversal path approach.\n\nThe goal of this project is to understand how we can make expression tree processing more efficient and how it can be plugged into larger systems, such as an ORM.\n\n---\n\n## Purpose\n\nWhen working with LINQ expressions like:\n\n```csharp\nint minAge = 30;\nExpression\u003cFunc\u003cUser, bool\u003e\u003e expr = x =\u003e x.Age \u003e minAge;\n```\n\nWe often need to extract the actual value (30) from inside the expression tree. While caching the translated SQL string avoids repeated query translation, we still need to re-traverse the expression tree on every execution to extract the latest values of captured variables.\n\nThis project explores how to optimize that specific part — extracting values efficiently without walking the entire tree each time.\n\n---\n\n## Two Approaches Compared\n\n### Visitor-Based Traversal (Classic)\n\nWalks the expression tree using `ExpressionVisitor`, checking each node type dynamically.\n\n### Cached Traversal Path (Optimized)\n\nBuilds a reusable map of how to reach the variable nodes, then extracts values efficiently without re-visiting the whole tree.\n\n---\n\n## Benchmark Results\n\n| Method                    | Mean     | StdDev   | Gain        |\n|---------------------------|----------|----------|-------------|\n| VisitorBasedExtraction    | 703.2 ns | 15.81 ns |             |\n| CachedTraversalExtraction | 374.1 ns |  5.60 ns | ~47% faster |\n\nTested using [BenchmarkDotNet](https://benchmarkdotnet.org/) on realistic LINQ expressions with method calls, captured values, and nested paths.\n\n---\n\n## What's in This Repo?\n\n| File                                | Purpose                                          |\n|-------------------------------------|--------------------------------------------------|\n| `ExpressionTraversalPath.cs`        | Stores precomputed traversal delegates           |\n| `ExpressionTraversalPathBuilder.cs` | Builds the cached path structure                 |\n| `VisitorBasedExtractor.cs`          | Classic visitor for comparison                   |\n| `ReflectionService.cs`              | Resolves values from member/unary expressions    |\n| `ExpressionValueBenchmark.cs`       | BenchmarkDotNet test setup                       |\n| `Program.cs`                        | Entry point run and inspect                      |\n\n---\n\n## License\n\nMIT License feel free to fork, study, extend, and share.\n\n---","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsallushan%2Flinq-variable-finder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsallushan%2Flinq-variable-finder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsallushan%2Flinq-variable-finder/lists"}