DynamicPredicateBuilder 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DynamicPredicateBuilder --version 1.0.0
                    
NuGet\Install-Package DynamicPredicateBuilder -Version 1.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="DynamicPredicateBuilder" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DynamicPredicateBuilder" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DynamicPredicateBuilder" />
                    
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 DynamicPredicateBuilder --version 1.0.0
                    
#r "nuget: DynamicPredicateBuilder, 1.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 DynamicPredicateBuilder@1.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=DynamicPredicateBuilder&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DynamicPredicateBuilder&version=1.0.0
                    
Install as a Cake Tool

DynamicPredicateBuilder 使用說明

簡介

DynamicPredicateBuilder 提供一套靈活的查詢條件組合機制,支援欄位查詢權限控管、動態過濾、排序與分頁,適用於 .NET 8 專案。


1. 欄位查詢權限設定

方式一:程式碼指定可查詢欄位

於 Controller 設定 AllowedFields,僅允許指定欄位查詢: [HttpPost("people")] public IActionResult QueryPeople([FromBody] QueryRequest request) { var options = new FilterOptions { AllowedFields = new HashSet<string> { "Name", "Age", "Address.City" } // 允許查的欄位 };

var filterGroup = FilterGroupFactory.FromDictionary(request.Filter);
var predicate = FilterBuilder.Build<Person>(filterGroup, options);

var query = _db.People.Where(predicate)
    .ApplySort(request.Sort);

var totalCount = query.Count();

var result = query.Skip((request.Page - 1) * request.PageSize)
                  .Take(request.PageSize)
                  .ToList();

return Ok(new QueryResult<Person>
{
    TotalCount = totalCount,
    Items = result
});

}

方式二:使用 Attribute 標註可查詢欄位

於 Model 屬性加上 [Queryable],自動取得可查詢欄位:

Model 使用方式public class Person

{ [Queryable] public string Name { get; set; }

[Queryable]
public int Age { get; set; }

public string Password { get; set; } // 沒標,不能查

[Queryable]
public Address Address { get; set; }

}

public class Address { [Queryable] public string City { get; set; }

public string SecretNote { get; set; } // 沒標,不能查

}

Controller 使用方式[HttpPost("people")]

public IActionResult QueryPeople([FromBody] QueryRequest request) { var allowedFields = QueryableFieldHelper.GetQueryableFields<Person>();

var options = new FilterOptions
{
    AllowedFields = allowedFields
};

var filterGroup = FilterGroupFactory.FromDictionary(request.Filter);
var predicate = FilterBuilder.Build<Person>(filterGroup, options);

var query = _db.People.Where(predicate)
    .ApplySort(request.Sort);

var totalCount = query.Count();

var result = query.Skip((request.Page - 1) * request.PageSize)
                  .Take(request.PageSize)
                  .ToList();

return Ok(new QueryResult<Person>
{
    TotalCount = totalCount,
    Items = result
});

}

取得可查詢欄位QueryableFieldHelper.GetQueryableFields<Person>();

2. API 使用方式

Request Example{

"Filter": { "LogicalOperator": "And", "Rules": [ { "Property": "Age", "Operator": "GreaterThanOrEqual", "Value": 25 }, { "Property": "Address.City", "Operator": "Equal", "Value": "Taipei" } ] }, "Sort": [ { "Property": "Name", "Descending": false }, { "Property": "Age", "Descending": true } ], "Page": 1, "PageSize": 5 }

Response Example{

"totalCount": 45, "items": [ { "name": "Alice", "age": 30, "address": { "city": "Taipei" } }, ... ] }


3. API 範例

Request 範例

Response 範例

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 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 is compatible.  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

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
1.0.6 184 9/24/2025
1.0.5 171 9/24/2025
1.0.4 143 9/2/2025
1.0.3 180 8/28/2025
1.0.2 151 7/8/2025
1.0.1 146 7/8/2025
1.0.0 149 7/8/2025