AutoQuery.AspNetCore 1.1.1

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

AutoQuery

GitHub Actions GitHub Actions Releases NuGet

AutoQuery is a powerful library for .NET that simplifies dynamic query building, filtering, and pagination using expression trees. It provides a flexible and extensible way to handle complex query scenarios. The AutoQuery.AspNetCore package extends this functionality with seamless integration into ASP.NET Core applications, offering middleware support and field projection capabilities.

Features

  • Dynamic Query Building: Generate queries dynamically using expression trees.
  • Filtering: Apply flexible filtering logic to refine query results.
  • Field Projection: Return only the specified fields to optimize API responses.
  • Pagination and Sorting: Built-in support for pagination and sorting.
  • ASP.NET Core Integration: Middleware support for easy integration into ASP.NET Core projects.

Benchmark

The benchmark results provide an overview of AutoQuery's performance in handling dynamic queries and filtering. These results can help evaluate its suitability for various application scenarios.

alternate text is missing from this package README image

Installation

AutoQuery

Install the core library via NuGet:

dotnet add package AutoQuery

AutoQuery.AspNetCore

For ASP.NET Core integration, install the extension package:

dotnet add package AutoQuery.AspNetCore

Getting Started

Using AutoQuery in .NET Applications

  1. Define a query options class implementing the IQueryOptions interface:
    public class UserQueryOptions : IQueryPagedOptions
    {
        public int[]? FilterIds { get; set; }
        public string? FilterName { get; set; }
        public string? Fields { get; set; }
        public string? Sort { get; set; }
        public int? Page { get; set; }
        public int? PageSize { get; set; }
    }
    
  2. Configure the filter logic by implementing IFilterQueryConfiguration:
     public class UserQueryConfiguration : IFilterQueryConfiguration<UserQueryOptions, User>
     {
         public void Configure(FilterQueryBuilder<UserQueryOptions, User> builder)
         {
             builder.Property(q => q.FilterIds, d => d.Id)
                 .HasCollectionContains();
             builder.Property(q => q.FilterName, d => d.Name)
                 .HasEqual();
         }
     }
    
  3. Use AutoQuery to process queries:
    var queryProcessor = new QueryProcessor();
    queryProcessor.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
    
    var users = new List<User>
    {
        new User(4, "Bob Brown", "bob.brown@example.com", new DateTime(1988, 12, 30)),
        new User(1, "John Doe", "john.doe@example.com", new DateTime(1990, 1, 1)),
        new User(3, "Alice Johnson", "alice.johnson@example.com", new DateTime(1992, 8, 23)),
        new User(5, "Charlie Davis", "charlie.davis@example.com", new DateTime(1995, 3, 10)),
        new User(2, "Jane Smith", "jane.smith@example.com", new DateTime(1985, 5, 15)),
    };
    
    var result = users.AsQueryable().ApplyQuery(queryProcessor, new UserQueryOptions()
    {
        Fields = "Id,Name",
        Sort = "-Id",
        FilterIds = new[] { 3, 4 },
        FilterName = "Alice Johnson"
    }).ToArray();
    
    Console.WriteLine("Filtered Users:");
    foreach (var user in result)
    {
        Console.WriteLine($"{user.Id}: {user.Name}");
    }
    
    1. Example Output:
    Filtered Users:
    3: Alice Johnson
    

Using AutoQuery.AspNetCore in ASP.NET Core Applications

  1. Define a query options class implementing the IQueryOptions interface:
    public class UserQueryOptions : IQueryPagedOptions
    {
        [FromQuery(Name = "filter[ids]")]
        public int[]? FilterIds { get; set; }
        [FromQuery(Name = "filter[name]")]
        public string? FilterName { get; set; }
        [FromQuery(Name = "fields")]
        public string? Fields { get; set; }
        [FromQuery(Name = "sort")]
        public string? Sort { get; set; }
        [FromQuery(Name = "page")]
        public int? Page { get; set; }
        [FromQuery(Name = "pageSize")]
        public int? PageSize { get; set; }
    }
    
  2. Configure the filter logic by implementing IFilterQueryConfiguration:
     public class UserQueryConfiguration : IFilterQueryConfiguration<UserQueryOptions, User>
     {
         public void Configure(FilterQueryBuilder<UserQueryOptions, User> builder)
         {
             builder.Property(q => q.FilterIds, d => d.Id)
                 .HasCollectionContains();
             builder.Property(q => q.FilterName, d => d.Name)
                 .HasEqual();
         }
     }
    
  3. Register the required services in Program.cs:
     // Add AutoQuery services
     builder.Services.AddAutoQuery(Assembly.GetEntryAssembly());
    
  4. Create a controller to handle queries:
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private readonly IQueryProcessor _queryProcessor;
    
        private List<User> users = new List<User>
        {
            new User(4, "Bob Brown", "bob.brown@example.com", new DateTime(1988, 12, 30)),
            new User(1, "John Doe", "john.doe@example.com", new DateTime(1990, 1, 1)),
            new User(3, "Alice Johnson", "alice.johnson@example.com", new DateTime(1992, 8, 23)),
            new User(5, "Charlie Davis", "charlie.davis@example.com", new DateTime(1995, 3, 10)),
            new User(2, "Jane Smith", "jane.smith@example.com", new DateTime(1985, 5, 15)),
        };
    
        public UsersController(IQueryProcessor queryProcessor)
        {
            _queryProcessor = queryProcessor;
        }
    
        [HttpGet]
        [EnableFieldProjection]
        public IActionResult Get(UserQueryOptions queryOptions)
        {
            var result = users.AsQueryable()
                              .ApplyQueryPagedResult(_queryProcessor, queryOptions);
            return Ok(result);
        }
    }
    
  5. Example Request:
GET /Users?filter[ids]=1&filter[ids]=3&fields=Id,Name&sort=-Id&page=1&pageSize=2
  1. Example Response:
{
    "datas": [
        {
            "id": 3,
            "name": "Alice Johnson"
        },
        {
            "id": 1,
            "name": "John Doe"
        }
    ],
    "count": 2,
    "totalPages": 1,
    "page": 1
}

Contribution

Contributions are welcome! Feel free to submit issues or pull requests to improve the project.

License

This project is licensed under the MIT License.

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

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.1.1 154 4/24/2025
1.1.0 218 4/22/2025
1.0.1 126 4/18/2025
1.0.0 161 4/18/2025