HLCores.Utilities.PredicateBuilder 4.0.0

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

HLCores.Utilities.PredicateBuilder

HLCores.Utilities.PredicateBuilder is a design pattern (often implemented as a utility class) that allows you to dynamically build LINQ expression trees at runtime — especially for Where() clauses with optional or conditional filters .

🎯 Purpose

To dynamically generate filtering conditions for LINQ depending on the type and value of param:

  • If param is a string → performs keyword search across common text fields.
  • If param is a single value → compares it with a property (default is "Id").
  • If param is a class object → builds combined conditions based on each property value, optionally enhanced with attributes.

✅ 1. param is a string (e.g. search keyword)To dynamically generate a .Where(...) filter like:

BuildPredicate<Product, string>("abc")
  • Filters by fields like: Code, Title, Description, Email, etc.
  • Uses .ToLower().Contains(keyword) to match values case-insensitively.

✅ 2. param is a single value (e.g. int, Guid)

BuildPredicate<User, Guid>(userId)
  • Creates simple comparison: x => x.Id == userId (or a different property if paramName is specified).

✅ 3. param is an object with multiple properties

BuildPredicate<Product, ProductFilter>(filter)
  • Each non-null property in filter is matched with the corresponding property in TModel.
  • Optional support for PredicateFilterAttribute allows you to define:
    • Comparison operator (=, LIKE, >, <, IN, etc.)
    • Logical operator (AND, OR)
    • Grouping of conditions using group (for compound filters)

🔧 Example of PredicateFilterAttribute

public class ProductFilter
{
    [PredicateFilter(CompareOperator = SqlCompareOperatorConstants.LIKE)]
    public string? Name { get; set; }

    [PredicateFilter(CompareOperator = SqlCompareOperatorConstants.GREATER_THAN_OR_EQUAL_TO)]
    public decimal? MinPrice { get; set; }

    [PredicateFilter(CompareOperator = SqlCompareOperatorConstants.LESS_THAN_OR_EQUAL_TO)]
    public decimal? MaxPrice { get; set; }
}
x => x.Name.Contains("...") && x.Price >= ... && x.Price <= ...

📦 Return value

  • If no valid conditions found → returns x => true (no filtering).
  • Otherwise → returns a composed predicate for .Where(...).

📘 Usage example

Expression<Func<Product, bool>> predicate = PredicateBuilderUtility.BuildPredicate<Product, ProductFilter>(filter);
var result = dbContext.Products.Where(predicate);
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.0 154 4/2/2026
3.0.0 128 3/28/2026
2.0.0 732 12/3/2025