PredicateMap 1.0.1

dotnet add package PredicateMap --version 1.0.1
                    
NuGet\Install-Package PredicateMap -Version 1.0.1
                    
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="PredicateMap" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PredicateMap" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="PredicateMap" />
                    
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 PredicateMap --version 1.0.1
                    
#r "nuget: PredicateMap, 1.0.1"
                    
#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 PredicateMap@1.0.1
                    
#: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=PredicateMap&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=PredicateMap&version=1.0.1
                    
Install as a Cake Tool

PredicateMap

NuGet

PredicateMap is a high-performance .NET library that allows you to map keys to values using predicates and direct key lookups. It compiles the mapping logic into an optimized expression tree, providing fast lookups even with complex conditions.

Features

  • Fast Lookups: Compiles mapping logic into a single delegate using Expression Trees.
  • Flexible Mapping: Supports both direct key-to-value mapping and predicate-based mapping.
  • Priority Support: PriorityPredicateMapBuilder allows defining order-sensitive mappings.
  • Fluent API: Easy-to-use builder pattern and extension methods.

Installation

Install the package via NuGet:

dotnet add package PredicateMap

Usage

Basic Usage with PredicateMapBuilder

Use PredicateMapBuilder when you have a mix of specific keys and general predicates.

using PredicateMap;
using System.Linq.Expressions;

var builder = new PredicateMapBuilder<int, string>()
    // Direct key mapping
    .Add(1, "One")
    .Add(2, "Two")
    // Predicate mapping
    .Add(x => x > 10, "Greater than ten")
    .Add(x => x < 0, "Negative");

IPredicateMap<int, string> map = builder.ToPredicateMap();

if (map.TryGet(1, out var value))
{
    Console.WriteLine(value); // Output: One
}

if (map.TryGet(15, out value))
{
    Console.WriteLine(value); // Output: Greater than ten
}

Priority Mapping with PriorityPredicateMapBuilder

Use PriorityPredicateMapBuilder when the order of evaluation matters. The first matching predicate or key will return the value.

using PredicateMap;

var builder = new PriorityPredicateMapBuilder<int, string>()
    // Order matters here
    .Add(x => x % 2 == 0, "Even")
    .Add(x => x % 3 == 0, "Divisible by 3")
    .Add(6, "Six"); // This will never be reached because 6 is Even

IPredicateMap<int, string> map = builder.ToPredicateMap();

map.TryGet(6, out var val1); // Output: Even
map.TryGet(9, out var val2); // Output: Divisible by 3

Creating from an Enumerable

You can also create a map from any IEnumerable<T> using the ToPredicateMap extension method.

var items = new[] { 1, 2, 3, 4, 5 };

IPredicateMap<int, string> map = items.ToPredicateMap(
    keySelector: x => x,
    valueSelector: x => x % 2 == 0 ? "Even" : "Odd"
);

Performance

PredicateMap is designed for performance. It compiles the lookup logic into a highly optimized delegate, minimizing overhead during runtime lookups. This makes it suitable for hot paths where performance is critical.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on PredicateMap:

Package Downloads
SwiftState

SwiftState is a lightweight, fluent state machine library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 138 1/20/2026
1.0.0 115 1/20/2026