https://github.com/styrainc/ucast-linq
UCAST integration for LINQ.
https://github.com/styrainc/ucast-linq
Last synced: about 1 year ago
JSON representation
UCAST integration for LINQ.
- Host: GitHub
- URL: https://github.com/styrainc/ucast-linq
- Owner: StyraInc
- License: apache-2.0
- Created: 2024-12-09T22:14:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-20T18:08:26.000Z (about 1 year ago)
- Last Synced: 2025-06-02T08:27:43.098Z (about 1 year ago)
- Language: C#
- Size: 78.1 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Styra UCAST integration for LINQ
[](https://opensource.org/licenses/Apache-2.0)
[](https://www.nuget.org/packages/Styra.Ucast.Linq/)
> [!IMPORTANT]
> The reference documentation for this library is available at https://styrainc.github.io/ucast-linq
## Installation
### Nuget
```bash
dotnet add package Styra.Ucast.Linq
```
## Example Usage
Let's assume that we have a collection of random integers, and wish to filter them with a LINQ query using multiple criteria:
```csharp
using System;
using System.Linq;
public record SimpleRecord(int Value);
var numbers = new int[] { -1523, 1894, -456, 789, -1002, 345, -1789, 567, 1234, -890, 123, -1456, 1678, -234, 567, -1890, 901, -345, 1567, -789 };
var collection = numbers.Select(n => new SimpleRecord(n)).ToList();
var results = collection.Where(x => x.Value >= 1500 || (x.Value < 400 && (x.Value > 0 || x.Value < -1500)))
.OrderBy(x => x.Value)
.ToList();
Console.WriteLine(string.Join("\n", results));
```
Output
```csharp
SimpleRecord { Value = -1890 }
SimpleRecord { Value = -1789 }
SimpleRecord { Value = -1523 }
SimpleRecord { Value = 123 }
SimpleRecord { Value = 345 }
SimpleRecord { Value = 1567 }
SimpleRecord { Value = 1678 }
SimpleRecord { Value = 1894 }
```
Using this library, the same filters can be constructed dynamically using UCAST expressions (which can be deserialized from JSON):
```csharp
using System;
using System.Linq;
using Styra.Ucast.Linq;
public record SimpleRecord(int Value);
var conditions = new UCASTNode { Type = "compound", Op = "or", Value = new List{
new UCASTNode { Type = "field", Op = "ge", Field = "r.value", Value = 1500 },
new UCASTNode { Type = "compound", Op = "and", Value = new List{
new UCASTNode { Type = "field", Op = "lt", Field = "r.value", Value = 400 },
new UCASTNode { Type = "compound", Op = "or", Value = new List{
new UCASTNode { Type = "field", Op = "gt", Field = "r.value", Value = 0 },
new UCASTNode { Type = "field", Op = "lt", Field = "r.value", Value = -1500 },
} },
} },
} };
var numbers = new int[] { -1523, 1894, -456, 789, -1002, 345, -1789, 567, 1234, -890, 123, -1456, 1678, -234, 567, -1890, 901, -345, 1567, -789 };
var collection = numbers.Select(n => new SimpleRecord(n)).ToList();
var results = collection.AsQueryable()
.ApplyUCASTFilter(conditions, QueryableExtensions.BuildDefaultMapperDictionary("r"))
.OrderBy(x => x.Value)
.ToList();
Console.WriteLine(string.Join("\n", results));
```
Output
```csharp
SimpleRecord { Value = -1890 }
SimpleRecord { Value = -1789 }
SimpleRecord { Value = -1523 }
SimpleRecord { Value = 123 }
SimpleRecord { Value = 345 }
SimpleRecord { Value = 1567 }
SimpleRecord { Value = 1678 }
SimpleRecord { Value = 1894 }
```
## Community
For questions, discussions and announcements related to Styra products, services and open source projects, please join
the Styra community on [Slack](https://communityinviter.com/apps/styracommunity/signup)!