SmartWhere

🚨 Frequently Asked Questions

Common questions, solutions, and troubleshooting information for SmartWhere.

❓ General Questions

Q: What is 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.

Q: What versions of .NET does SmartWhere support?

A: SmartWhere supports:

Q: Is SmartWhere free to use?

A: Yes, SmartWhere is open-source and available under the MIT License, making it free for both personal and commercial use.

Q: Can I use SmartWhere with Entity Framework Core?

A: Yes! SmartWhere is designed to work seamlessly with Entity Framework Core and any other IQueryable<T> implementation.

🔧 Installation & Setup

Q: How do I install SmartWhere?

A: Install via NuGet:

# Package Manager Console
PM> Install-Package SmartWhere

# .NET CLI
dotnet add package SmartWhere

# NuGet Package Manager
Install-Package SmartWhere

Q: Do I need to add any using statements?

A: Yes, add this using statement to your files:

using SmartWhere;

Q: Is there any configuration required?

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;
});

🏷️ Attributes & Usage

Q: What is the difference between WhereClause and TextualWhereClause?

A:

Q: How do I use OR logic instead of AND?

A: Set the LogicalOperator property:

[WhereClause(LogicalOperator.OR)]
public string Title { get; set; }

Q: Can I filter on nested properties?

A: Yes! Use dot notation:

[WhereClause("Author.Name")]
public string AuthorName { get; set; }

[WhereClause("Publisher.Country.Name")]
public string CountryName { get; set; }

Q: What’s the maximum nesting depth?

A: SmartWhere supports up to 4 levels of nesting in version 2.0+, though deeper nesting may impact performance.

🚨 Common Issues & Solutions

Q: I get “Property not found” errors. What’s wrong?

A: Common causes:

  1. Missing Include statements: ```csharp // ❌ Wrong - missing Include var result = _context.Books.Where(request).ToList();

// ✅ 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; }
  1. Missing interface implementation: ```csharp // ❌ Wrong - missing IWhereClause public class SearchRequest { [WhereClause] 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
}

Q: Can I use SmartWhere with async operations?

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);
}

⚡ Performance Questions

Q: Is SmartWhere fast?

A: Yes! SmartWhere has minimal performance overhead:

Q: How can I optimize performance?

A: Performance tips:

  1. Limit nested property depth: ```csharp // ❌ Deep nesting (slower) [WhereClause(“Publisher.Country.Region.City.Name”)]

// ✅ 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();
  1. Only include necessary properties:
    // ❌ Don't include unused properties
    public class InefficientRequest : IWhereClause
    {
     [WhereClause]
     public string Title { get; set; }
        
     public string SortBy { get; set; }  // Not used for filtering
    }
    

Q: Does SmartWhere work with database indexes?

A: Yes! SmartWhere generates standard SQL WHERE clauses that can utilize database indexes. Ensure your filtered properties are properly indexed.

🔧 Advanced Usage

Q: Can I create custom attributes?

A: Yes! Inherit from base classes:

public class CustomWhereClauseAttribute : WhereClauseAttribute
{
    public string CustomProperty { get; set; }
    
    public CustomWhereClauseAttribute(string customProperty)
    {
        CustomProperty = customProperty;
    }
}

Q: How do I combine multiple filters with logical operators?

A: Use the extension methods:

var result = _context.Books
    .Where(request1)
    .And(request2)
    .Or(request3)
    .Not(request4)
    .ToList();

Q: Can I use SmartWhere with stored procedures?

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.

Q: How do I handle 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();

🐛 Debugging & Troubleshooting

Q: How can I see the generated SQL?

A: Enable Entity Framework logging:

// In Program.cs
services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString)
           .LogTo(Console.WriteLine, LogLevel.Information));

Q: My filters are too restrictive. How do I debug?

A: Debug step by step:

  1. Start with one filter:
    var result = _context.Books.Where(request).ToList();
    
  2. Add filters incrementally:
    var result = _context.Books
     .Where(b => b.Title.Contains("test"))  // Test manually first
     .Where(request)                        // Then add SmartWhere
     .ToList();
    
  3. Check property values:
    // Log the request object
    Console.WriteLine(JsonSerializer.Serialize(request));
    

Q: How do I handle validation errors?

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}");
}

🔗 Integration Questions

Q: Does SmartWhere work with AutoMapper?

A: Yes! SmartWhere works with any IQueryable<T>, including AutoMapper projections.

Q: Can I use SmartWhere with GraphQL?

A: Yes! SmartWhere can be used in GraphQL resolvers that work with IQueryable<T>.

Q: Does SmartWhere support localization?

A: SmartWhere itself doesn’t handle localization, but you can use localized property names in your attributes.

📚 Getting Help

Q: Where can I find more examples?

A: Check these resources:

Q: How do I report a bug?

A: Report bugs on GitHub:

  1. Go to Issues
  2. Click “New Issue”
  3. Provide detailed information about the problem

Q: Can I contribute to SmartWhere?

A: Yes! Contributions are welcome:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

Last Updated: 2025-01-16
Version: 2.2.3