LinqAPI 1.0.0

Additional Details

LinqApi V1.1 will be released on 15th of May

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

LinqAPI

LinqAPI is a dynamic and generic API generator for ASP.NET Core that leverages LINQ-based filtering, paging, and CRUD operations. It provides a set of reusable controllers, services, and repository implementations to reduce boilerplate code and streamline API development by automatically mapping between your entities and DTOs.


Table of Contents


Features

  • Dynamic CRUD Operations: Automatically handles common CRUD operations.
  • LINQ-Based Filtering & Paging: Supports dynamic filtering using LINQ expressions and returns paginated results.
  • Automatic Mapping: Uses AutoMapper with a dynamic mapping profile to map between your entities and DTOs without manual configuration.
  • Generic & Extensible: Works with any entity/DTO pair that derives from BaseEntity<TId> and BaseDto<TId>.
  • DI Registration Helper: Simplifies dependency injection configuration via a single extension method (builder.Services.AddLinqApi<DbContext>()).

Getting Started

Prerequisites

Installation

You can add LinqAPI to your project via NuGet:

dotnet add package LinqAPI

Or, if you're developing it locally, clone the repository and add it as a project reference.

DI Registration

LinqAPI provides an extension method to simplify DI registration. In your Program.cs (or Startup.cs), add:

using LinqAPI;

var builder = WebApplication.CreateBuilder(args);

// Register your DbContext
builder.Services.AddDbContext<DemoDbContext>(options =>
    options.UseInMemoryDatabase("DemoDb"));

// Register LinqAPI dependencies, including repository, service, and automatic mapping
builder.Services.AddLinqApi<DemoDbContext>();

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();

Filtering and Paging

LinqAPI provides powerful filtering and paging capabilities using dynamic LINQ queries. You can perform filtering using expressions such as StartsWith, EndsWith, Contains, and logical operators like and, or, >, <, >=, <=.

Example Request for filterpaged Endpoint

{
  "filter": "name.StartsWith(\"Product C\")",
  "pager": {
    "pageNumber": 1,
    "pageSize": 50
  },
  "orderby": "id",
  "desc": true
}

How Filtering Works

The filter field is a dynamic LINQ expression that is executed against the database.

  • StartsWith("value") – Matches strings that start with the given value.
  • EndsWith("value") – Matches strings that end with the given value.
  • Contains("value") – Matches strings that contain the given value.
  • > < >= <= – Comparison operators.
  • and, or – Logical operators.
  • 1=1 – Used to retrieve all records without filtering.
Example Queries:

Retrieve products where price is greater than 20 and name contains "Product":

{
  "filter": "price > 20 and name.Contains(\"Product\")"
}

Retrieve items where ID is less than 5 or the name starts with "Item":

{
  "filter": "id < 5 or name.StartsWith(\"Item\")"
}

Paging System

Paging is controlled using the Pager class:

public class Pager
{
    private int _pageNumber = 1;
    private int _pageSize = 50;

    public int PageNumber
    {
        get => _pageNumber;
        set => _pageNumber = value < 1 ? 1 : value > int.MaxValue ? int.MaxValue : value;
    }

    public int PageSize
    {
        get => _pageSize;
        set => _pageSize = value < 1 ? 1 : value > 500 ? 500 : value;
    }
}
  • PageNumber: Defines the current page (default = 1).
  • PageSize: Defines the number of results per page (min = 1, max = 500).

Retrieve all records without filtering:

{
  "filter": "1=1",
  "pager": {
    "pageNumber": 1,
    "pageSize": 50
  }
}

Sorting with orderby & desc

  • orderby: The property name to sort results by.
  • desc: Boolean flag for descending order (true for descending, false for ascending).

Including Navigation Properties

The Includes field allows eager loading of related navigation properties.

public class LinqFilterModel
{
    public string Filter { get; set; }
    public Pager Pager { get; set; }
    public List<string> Includes { get; set; }
    public string Orderby { get; set; }
    public bool Desc { get; set; }
}
Example Request with Includes:
{
  "filter": "1=1",
  "pager": {
    "pageNumber": 1,
    "pageSize": 50
  },
  "includes": ["Category", "Supplier"]
}

This will include Category and Supplier navigation properties in the response.


Conclusion

LinqAPI simplifies API development by providing automated filtering, paging, and sorting using LINQ expressions. With minimal setup, you can create powerful, dynamic APIs.

For any issues, feature requests, or questions, please open an issue in the repository or contact the maintainers.

Happy coding! 🚀

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.0.1 459 3/24/2025
1.0.0 348 3/5/2025 1.0.0 is deprecated because it is no longer maintained.