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
<PackageReference Include="HLCores.Utilities.PredicateBuilder" Version="4.0.0" />
<PackageVersion Include="HLCores.Utilities.PredicateBuilder" Version="4.0.0" />
<PackageReference Include="HLCores.Utilities.PredicateBuilder" />
paket add HLCores.Utilities.PredicateBuilder --version 4.0.0
#r "nuget: HLCores.Utilities.PredicateBuilder, 4.0.0"
#:package HLCores.Utilities.PredicateBuilder@4.0.0
#addin nuget:?package=HLCores.Utilities.PredicateBuilder&version=4.0.0
#tool nuget:?package=HLCores.Utilities.PredicateBuilder&version=4.0.0
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
paramis a string → performs keyword search across common text fields. - If
paramis a single value → compares it with a property (default is"Id"). - If
paramis 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 ifparamNameis specified).
✅ 3. param is an object with multiple properties
BuildPredicate<Product, ProductFilter>(filter)
- Each non-null property in
filteris matched with the corresponding property inTModel. - Optional support for
PredicateFilterAttributeallows you to define:- Comparison operator (
=,LIKE,>,<,IN, etc.) - Logical operator (
AND,OR) - Grouping of conditions using
group(for compound filters)
- Comparison operator (
🔧 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 | Versions 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. |
-
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.