It intelligently filters a sequence of items in a simple and practical way.
$ dotnet add package SmartWhereSmartWhere is a production-ready .NET library that provides intelligent filtering capabilities for IQueryable<T> collections. It transforms complex filtering logic into simple, declarative code using attributes and interfaces, making your data access layer cleaner and more maintainable.
Books.Author.Name)IQueryable<T> implementationInstall the SmartWhere NuGet package:
# Package Manager Console
PM> Install-Package SmartWhere
# .NET CLI
dotnet add package SmartWhere
# NuGet Package Manager
Install-Package SmartWhere
IWhereClause:public class PublisherSearchRequest : IWhereClause
{
[WhereClause]
public int Id { get; set; }
[WhereClause(PropertyName = "Name")]
public string PublisherName { get; set; }
[WhereClause("Book.Name")]
public string BookName { get; set; }
[WhereClause("Books.Author.Name")]
public string AuthorName { get; set; }
}
[HttpPost]
public IActionResult GetPublishers(PublisherSearchRequest request)
{
var result = _context.Set<Publisher>()
.Include(x => x.Books)
.ThenInclude(x => x.Author)
.Where(request) // 🎯 SmartWhere magic happens here!
.ToList();
return Ok(result);
}
That's it! SmartWhere automatically generates the appropriate WHERE clauses based on your request object.
┌─────────────────────────────────────────────────────────────┐
│ SmartWhere Library │
├─────────────────────────────────────────────────────────────┤
│ 📋 Core Components │
│ • WhereClauseAttribute • IWhereClause Interface │
│ • Extensions • Logical Operators │
│ • Comparison Operators • String Methods │
├─────────────────────────────────────────────────────────────┤
│ 🔧 Extension Methods │
│ • Where(request) • And(request) │
│ • Or(request) • Not(request) │
├─────────────────────────────────────────────────────────────┤
│ 🎯 Attribute System │
│ • WhereClause • TextualWhereClause │
│ • ComparativeWhereClause • WhereClauseClass │
└─────────────────────────────────────────────────────────────┘
public class BookSearchRequest : IWhereClause
{
[TextualWhereClause(StringMethod.Contains, PropertyName = "Title")]
public string Title { get; set; }
[TextualWhereClause(StringMethod.StartsWith, PropertyName = "ISBN")]
public string ISBN { get; set; }
[TextualWhereClause(StringMethod.EndsWith, PropertyName = "Description")]
public string Description { get; set; }
}
public class OrderSearchRequest : IWhereClause
{
[ComparativeWhereClause(ComparisonOperator.GreaterThan, PropertyName = "TotalAmount")]
public decimal MinAmount { get; set; }
[ComparativeWhereClause(ComparisonOperator.LessThanOrEqual, PropertyName = "OrderDate")]
public DateTime MaxDate { get; set; }
[ComparativeWhereClause(ComparisonOperator.Between, PropertyName = "Quantity")]
public int QuantityRange { get; set; }
}
// Combine multiple filters with logical operators
var result = _context.Orders
.Where(request1)
.And(request2)
.Or(request3)
.Not(request4)
.ToList();
public class AdvancedSearchRequest : IWhereClause
{
[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; }
}
git clone https://github.com/byerlikaya/SmartWhere.git
cd SmartWhere
dotnet restore
dotnet build
dotnet test
# Run all tests
dotnet test
# Run specific test project
dotnet test tests/SmartWhere.Tests/
# Run with coverage
dotnet test --collect:"XPlat Code Coverage"
cd sample/Sample.Api
dotnet run
Browse to the API endpoints to see SmartWhere in action.
// In Program.cs or Startup.cs
services.Configure<SmartWhereOptions>(options =>
{
options.DefaultStringMethod = StringMethod.Contains;
options.CaseSensitive = false;
options.MaxNestingLevel = 10;
});
[WhereClauseClass(DefaultStringMethod = StringMethod.StartsWith)]
public class CustomSearchRequest : IWhereClause
{
[WhereClause]
public string Name { get; set; }
}
| Attribute | Description | Example |
|---|---|---|
WhereClause | Basic property filtering | [WhereClause] |
TextualWhereClause | Text search with methods | [TextualWhereClause(StringMethod.Contains)] |
ComparativeWhereClause | Numeric/date comparisons | [ComparativeWhereClause(ComparisonOperator.GreaterThan)] |
WhereClauseClass | Class-level configuration | [WhereClauseClass] |
| Method | Description | SQL Equivalent |
|---|---|---|
Contains | Substring search | LIKE '%value%' |
StartsWith | Prefix search | LIKE 'value%' |
EndsWith | Suffix search | LIKE '%value' |
Equals | Exact match | = 'value' |
| Operator | Description | SQL Equivalent |
|---|---|---|
Equals | Equal to | = |
NotEquals | Not equal to | != |
GreaterThan | Greater than | > |
LessThan | Less than | < |
GreaterThanOrEqual | Greater than or equal | >= |
LessThanOrEqual | Less than or equal | <= |
Between | Range check | BETWEEN |
We welcome contributions! Please see our Contributing Guide for details.
git checkout -b feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.
IQueryable<T> foundationBuilt with ❤️ by Barış Yerlikaya
Made in Turkey 🇹🇷 | Contact | LinkedIn
⭐ Star this repository if you find SmartWhere helpful! ⭐