Common questions, solutions, and troubleshooting information for SmartWhere.
A: SmartWhere is a .NET library that provides intelligent filtering capabilities for IQueryable<T>
collections. It transforms complex filtering logic into simple, declarative code using attributes and interfaces.
A: SmartWhere supports:
A: Yes, SmartWhere is open-source and available under the MIT License, making it free for both personal and commercial use.
A: Yes! SmartWhere is designed to work seamlessly with Entity Framework Core and any other IQueryable<T>
implementation.
A: Install via NuGet:
# Package Manager Console
PM> Install-Package SmartWhere
# .NET CLI
dotnet add package SmartWhere
# NuGet Package Manager
Install-Package SmartWhere
A: Yes, add this using statement to your files:
using SmartWhere;
A: No, SmartWhere works out of the box. However, you can configure global options if needed:
services.Configure<SmartWhereOptions>(options =>
{
options.DefaultStringMethod = StringMethod.Contains;
options.CaseSensitive = false;
});
A:
WhereClause
: Basic property filtering with exact matchesTextualWhereClause
: Advanced text search with methods like Contains, StartsWith, EndsWithA: Set the LogicalOperator
property:
[WhereClause(LogicalOperator.OR)]
public string Title { get; set; }
A: Yes! Use dot notation:
[WhereClause("Author.Name")]
public string AuthorName { get; set; }
[WhereClause("Publisher.Country.Name")]
public string CountryName { get; set; }
A: SmartWhere supports up to 4 levels of nesting in version 2.0+, though deeper nesting may impact performance.
A: Common causes:
// ✅ Correct - with Include var result = _context.Books .Include(b => b.Author) .Include(b => b.Publisher) .Where(request) .ToList();
2. **Incorrect property names:**
```csharp
// ❌ Wrong - property doesn't exist
[WhereClause("NonExistentProperty")]
public string Name { get; set; }
// ✅ Correct - property exists
[WhereClause("Name")]
public string Name { get; set; }
// ✅ Correct - implements IWhereClause public class SearchRequest : IWhereClause { [WhereClause] public string Name { get; set; } }
### Q: My filters aren't working. What should I check?
**A:** Troubleshooting checklist:
1. ✅ Class implements `IWhereClause`
2. ✅ Properties have correct attributes
3. ✅ Property names match entity properties
4. ✅ Include statements for nested properties
5. ✅ Nullable properties for optional filters
6. ✅ Correct enum values for specialized attributes
### Q: How do I handle null values in filters?
**A:** Use nullable types and SmartWhere will automatically handle null values:
```csharp
public class SafeSearchRequest : IWhereClause
{
[WhereClause]
public string? Title { get; set; } // Optional filter
[WhereClause]
public int? PublishedYear { get; set; } // Optional filter
}
A: Yes! SmartWhere works with async operations:
public async Task<IActionResult> SearchBooks(BookSearchRequest request)
{
var books = await _context.Books
.Include(b => b.Author)
.Where(request)
.ToListAsync();
return Ok(books);
}
A: Yes! SmartWhere has minimal performance overhead:
A: Performance tips:
// ✅ Shallow nesting (faster) [WhereClause(“Publisher.CityName”)]
2. **Use projection for large result sets:**
```csharp
var result = _context.Books
.Where(request)
.Select(b => new { b.Title, b.Author.Name })
.ToList();
// ❌ Don't include unused properties
public class InefficientRequest : IWhereClause
{
[WhereClause]
public string Title { get; set; }
public string SortBy { get; set; } // Not used for filtering
}
A: Yes! SmartWhere generates standard SQL WHERE clauses that can utilize database indexes. Ensure your filtered properties are properly indexed.
A: Yes! Inherit from base classes:
public class CustomWhereClauseAttribute : WhereClauseAttribute
{
public string CustomProperty { get; set; }
public CustomWhereClauseAttribute(string customProperty)
{
CustomProperty = customProperty;
}
}
A: Use the extension methods:
var result = _context.Books
.Where(request1)
.And(request2)
.Or(request3)
.Not(request4)
.ToList();
A: SmartWhere works with IQueryable<T>
. If you can convert stored procedure results to IQueryable<T>
, then yes. Otherwise, consider using SmartWhere for the initial query and stored procedures for complex business logic.
A: Combine SmartWhere with additional LINQ operations:
var result = _context.Books
.Where(request)
.Where(b => b.IsActive && !b.IsDeleted)
.OrderBy(b => b.Title)
.ToList();
A: Enable Entity Framework logging:
// In Program.cs
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString)
.LogTo(Console.WriteLine, LogLevel.Information));
A: Debug step by step:
var result = _context.Books.Where(request).ToList();
var result = _context.Books
.Where(b => b.Title.Contains("test")) // Test manually first
.Where(request) // Then add SmartWhere
.ToList();
// Log the request object
Console.WriteLine(JsonSerializer.Serialize(request));
A: SmartWhere throws descriptive exceptions. Handle them appropriately:
try
{
var result = _context.Books.Where(request).ToList();
}
catch (PropertyNotFoundException ex)
{
// Handle missing property
_logger.LogError($"Property not found: {ex.PropertyName}");
}
catch (InvalidOperationException ex)
{
// Handle other validation errors
_logger.LogError($"Validation error: {ex.Message}");
}
A: Yes! SmartWhere works with any IQueryable<T>
, including AutoMapper projections.
A: Yes! SmartWhere can be used in GraphQL resolvers that work with IQueryable<T>
.
A: SmartWhere itself doesn’t handle localization, but you can use localized property names in your attributes.
A: Check these resources:
A: Report bugs on GitHub:
A: Yes! Contributions are welcome:
Last Updated: 2025-01-16
Version: 2.2.3