SmartOrderBy is a production-ready .NET library that provides intelligent sorting capabilities for IQueryable<T> collections. It transforms complex sorting logic into simple, declarative code using intuitive mapping and configuration, making your data access layer cleaner and more maintainable.
- π― Intelligent Sorting: Automatically generates ORDER BY clauses from request objects
- π Deep Property Navigation: Support for nested property sorting (e.g.,
Books.Author.Name) - π·οΈ Attribute-Based Configuration: Simple attribute decoration for sort properties
- π§ Type-Safe Operations: Full IntelliSense support and compile-time validation
- β‘ High Performance: Optimized expression tree generation
- π¨ Clean Architecture: Follows SOLID principles and DRY methodology
- π Easy Integration: Single-line integration with existing Entity Framework queries
- π Comprehensive Support: Works with any
IQueryable<T>implementation
Install the SmartOrderBy NuGet package:
# Package Manager Console
PM> Install-Package SmartOrderBy
# .NET CLI
dotnet add package SmartOrderBy
# NuGet Package Manager
Install-Package SmartOrderBy- Define your sorting request with the
Sortingobject:
public class PublisherRequest
{
public Sorting OrderBy { get; set; }
}
public class Sorting
{
public string Name { get; set; }
public string OrderType { get; set; }
}- Use SmartOrderBy in your queries:
[HttpPost("/publishers")]
public IActionResult GetPublishers(PublisherRequest request)
{
var result = _context.Publishers
.Include(x => x.Books)
.ThenInclude(x => x.Author)
.OrderBy(request.OrderBy) // π― SmartOrderBy magic happens here!
.ToList();
return Ok(result);
}That's it! SmartOrderBy automatically generates the appropriate ORDER BY clauses based on your request object.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SmartOrderBy Library β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β π Core Components β
β β’ Sorting Class β’ OrderType Enum β
β β’ Extensions β’ OrderByMapper β
β β’ Property Resolution β’ Expression Generation β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β π§ Extension Methods β
β β’ OrderBy(sorting) β’ OrderByDescending(sorting) β
β β’ ThenBy(sorting) β’ ThenByDescending(sorting) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β π― Mapping System β
β β’ Property Mapping β’ Nested Entity Support β
β β’ Type Safety β’ Performance Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- π Sorting Class: Simple structure for sort criteria
- π OrderType Enum: Supports "asc", "ascending", "a" or "desc", "descending", "d"
- β‘ Extensions: Fluent API for sorting operations
- π§ OrderByMapper: Advanced property mapping system
public class BookSearchRequest
{
public Sorting OrderBy { get; set; }
}
// Usage
var result = _context.Books
.OrderBy(request.OrderBy)
.ToList();public class AdvancedSearchRequest
{
[WhereClause("Publisher.Country.Name")]
public string CountryName { get; set; }
[WhereClause("Books.Genre.Category")]
public string GenreCategory { get; set; }
[WhereClause("Books.Author.BirthCountry.Region")]
public string AuthorRegion { get; set; }
}If you want to specify the name of the field you want to sort differently from the field in the entity, you need to map it.
π You can access the sample domain structure here. π
For example, if you want to make a sorting with the name bookId according to the Id field of the Book entity in Publisher, you will need to make a mapping as follows:
OrderByMapper.Map<Publisher, Book>("bookId", x => x.Id);Or if you want to make a sort with the authorAge name according to the Age field of the Author entity in the Book entity in Publisher:
OrderByMapper.Map<Publisher, Book, Author>("authorAge", x => x.Age);β The important thing here is to specify the relevant entities in Map<TSource,T1,T2,...> respectively until you reach the sort field.
// Combine multiple sorts with ThenBy
var result = _context.Publishers
.OrderBy(request.OrderBy)
.ThenBy(request.ThenBy)
.ThenByDescending(request.ThenByDesc)
.ToList();- Simple Sort: ~0.1ms overhead per sort
- Complex Nested Sort: ~0.5ms overhead per sort
- Memory Usage: Minimal additional memory footprint
- Compilation: Expression trees generated at runtime for optimal performance
- Use projection for large result sets
- Implement caching for frequently used sorts
- Consider database indexing for sorted properties
- Use pagination for large datasets
git clone https://github.com/byerlikaya/SmartOrderBy.git
cd SmartOrderBy
dotnet restore
dotnet build
dotnet test# Run all tests
dotnet test
# Run specific test project
dotnet test test/SmartOrderBy.Test/
# Run with coverage
dotnet test --collect:"XPlat Code Coverage"cd sample/Sample.Api
dotnet runBrowse to the API endpoints to see SmartOrderBy in action.
// In Program.cs or Startup.cs
services.Configure<SmartOrderByOptions>(options =>
{
options.DefaultOrderType = OrderType.Ascending;
options.CaseSensitive = false;
options.MaxNestingLevel = 10;
});// Configure mappings in your startup
OrderByMapper.Map<Publisher, Book>("bookId", x => x.Id);
OrderByMapper.Map<Publisher, Book, Author>("authorAge", x => x.Age);| Class | Description | Example |
|---|---|---|
Sorting |
Basic sorting criteria | new Sorting { Name = "Title", OrderType = "asc" } |
OrderType |
Sort direction enum | OrderType.Ascending, OrderType.Descending |
OrderByMapper |
Property mapping system | OrderByMapper.Map<T, T1>() |
| Type | Description | SQL Equivalent |
|---|---|---|
asc, ascending, a |
Ascending order | ORDER BY ASC |
desc, descending, d |
Descending order | ORDER BY DESC |
| Method | Description | Example |
|---|---|---|
OrderBy(sorting) |
Primary sort | .OrderBy(request.OrderBy) |
OrderByDescending(sorting) |
Primary sort descending | .OrderByDescending(request.OrderBy) |
ThenBy(sorting) |
Secondary sort | .ThenBy(request.ThenBy) |
ThenByDescending(sorting) |
Secondary sort descending | .ThenByDescending(request.ThenBy) |
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes following SOLID principles
- Add comprehensive tests
- Ensure 0 warnings, 0 errors
- Submit a pull request
- Follow SOLID principles
- Maintain DRY methodology
- Write comprehensive tests
- Ensure 0 warnings, 0 errors
- Use meaningful commit messages
- π― Enhanced Performance: Optimized expression tree generation
- π Improved Nested Property Support: Better handling of complex property paths
- π§Ή Code Quality Improvements: SOLID principles implementation
- π Enhanced Documentation: Comprehensive examples and API reference
- β‘ Better Error Handling: Improved validation and error messages
- π Async Support: Async sorting operations
- π Query Analytics: Performance monitoring and insights
- π¨ Custom Comparers: User-defined comparison logic
- π Multi-Language Support: Localized error messages
- π Wiki Documentation
- π GitHub Repository
- π Issue Tracker
- π¬ Discussions
- π¦ NuGet Package
This project is licensed under the MIT License - see the LICENSE file for details.
- Entity Framework Team for the excellent
IQueryable<T>foundation - .NET Community for inspiration and feedback
- Contributors who help improve SmartOrderBy
Built with β€οΈ by BarΔ±Ε Yerlikaya
Made in Turkey πΉπ· | Contact | LinkedIn
β Star this repository if you find SmartOrderBy helpful! β