🧭 Introduction
In modern APIs, users expect features like filtering, sorting, and pagination on list endpoints. Manually writing this logic for every query can get messy fast—especially when your models evolve.
Enter Sieve — a lightweight, declarative, and extensible NuGet package that lets you offload that complexity. Whether you're using Entity Framework, Dapper, or even in-memory data, Sieve handles query logic dynamically based on query string inputs.
In this article, we’ll:
-
✅ Build a real-world API using in-memory data
-
✅ Use
SieveModelto drive dynamic query handling -
✅ Add custom filters (like
IsAdult==true) -
✅ Organize everything in a clean, scalable structure
Filtering, Sorting & Pagination with Sieve
🚀 Step 1: Setup the Demo API
Create a new .NET Web API project.
dotnet new webapi -n SieveInMemoryDemo
cd SieveInMemoryDemo
dotnet add package Sieve
📁 Project Structure: Organizing Your Sieve API
Keeping your code organized is just as important as functionality. Here’s a clean project layout we’ll use:
SieveInMemoryDemo/
├── Controllers/
│ └── UsersController.cs
├── Models/
│ └── User.cs
├── Filters/
│ └── CustomSieveFilters.cs
├── Data/
│ └── DataStore.cs
├── Program.cs
├── SieveInMemoryDemo.csproj
└── appsettings.json
This separation ensures:
-
Clean controllers (or minimal endpoints)
-
Easy extension for EF Core or real DB
-
Flexible and testable components
🧩 Step 2: Create the User Model
using Sieve.Attributes;
public class User
{
public int Id { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
public string Name { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
public int Age { get; set; }
[Sieve(CanSort = true)]
public DateTime CreatedAt { get; set; }
}
🗃️ Step 3: Create the In-Memory Data Store
public class DataStore
{
public List<User> Users { get; } = new()
{
new User { Id = 1, Name = "Alice", Age = 28, CreatedAt = DateTime.Now.AddDays(-3) },
new User { Id = 2, Name = "Bob", Age = 35, CreatedAt = DateTime.Now.AddDays(-10) },
new User { Id = 3, Name = "Charlie", Age = 22, CreatedAt = DateTime.Now.AddDays(-1) },
new User { Id = 4, Name = "Diana", Age = 40, CreatedAt = DateTime.Now.AddDays(-5) },
new User { Id = 5, Name = "Eve", Age = 30, CreatedAt = DateTime.Now }
};
}
📂 Step 3: Add a Custom Filter (Optional)


Join the conversation! Your thoughts help the community grow.