OpenPolicyAgent.Ucast.Linq 0.6.0

dotnet add package OpenPolicyAgent.Ucast.Linq --version 0.6.0
                    
NuGet\Install-Package OpenPolicyAgent.Ucast.Linq -Version 0.6.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenPolicyAgent.Ucast.Linq" Version="0.6.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenPolicyAgent.Ucast.Linq" Version="0.6.0" />
                    
Directory.Packages.props
<PackageReference Include="OpenPolicyAgent.Ucast.Linq" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenPolicyAgent.Ucast.Linq --version 0.6.0
                    
#r "nuget: OpenPolicyAgent.Ucast.Linq, 0.6.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenPolicyAgent.Ucast.Linq@0.6.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenPolicyAgent.Ucast.Linq&version=0.6.0
                    
Install as a Cake Addin
#tool nuget:?package=OpenPolicyAgent.Ucast.Linq&version=0.6.0
                    
Install as a Cake Tool

EOPA Data Filters Support - C# UCAST integration for LINQ

License NuGet Version

The reference documentation for this library is available at https://open-policy-agent.github.io/ucast-linq

Installation

Nuget

dotnet add package OpenPolicyAgent.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:

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));

<details>

<summary>Output</summary>

SimpleRecord { Value = -1890 }
SimpleRecord { Value = -1789 }
SimpleRecord { Value = -1523 }
SimpleRecord { Value = 123 }
SimpleRecord { Value = 345 }
SimpleRecord { Value = 1567 }
SimpleRecord { Value = 1678 }
SimpleRecord { Value = 1894 }

</details>

Using this library, the same filters can be constructed dynamically using UCAST expressions (which can be deserialized from JSON):

using System;
using System.Linq;
using OpenPolicyAgent.Ucast.Linq;

public record SimpleRecord(int Value);

var conditions = new UCASTNode { Type = "compound", Op = "or", Value = new List<UCASTNode>{
    new UCASTNode { Type = "field", Op = "ge", Field = "r.value", Value = 1500 },
    new UCASTNode { Type = "compound", Op = "and", Value = new List<UCASTNode>{
        new UCASTNode { Type = "field", Op = "lt", Field = "r.value", Value = 400 },
        new UCASTNode { Type = "compound", Op = "or", Value = new List<UCASTNode>{
            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<SimpleRecord>("r"))
                        .OrderBy(x => x.Value)
                        .ToList();

Console.WriteLine(string.Join("\n", results));

<details>

<summary>Output</summary>

SimpleRecord { Value = -1890 }
SimpleRecord { Value = -1789 }
SimpleRecord { Value = -1523 }
SimpleRecord { Value = 123 }
SimpleRecord { Value = 345 }
SimpleRecord { Value = 1567 }
SimpleRecord { Value = 1678 }
SimpleRecord { Value = 1894 }

</details>

Community

For questions, discussions, and announcements, please join the OPA community on Slack!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on OpenPolicyAgent.Ucast.Linq:

Package Downloads
OpenPolicyAgent.Opa

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.6.0 294 8/29/2025