Skip to content

QueryBuilder

The QueryBuilder class is useful when you want to manually build your query or when you prefer not to use the extension methods.

MethodDescription
AddConditionAdds a string-based filtering query
AddOrderByAdds a string-based ordering query
ConfigurePagingConfigures Page and PageSize
AddQueryAccepts a GridifyQuery object to configure filtering, ordering, and paging
UseCustomMapperAccepts a GridifyMapper to use in build methods
UseEmptyMapperSets up an empty GridifyMapper without auto-generated mappings
AddMapAdds a single map to the existing mapper
AddCompositeMapAdds a composite map that searches across multiple properties with OR logic
AddNestedMapperAdds a nested mapper to reuse mappings from nested object types
RemoveMapRemoves a single map from the existing mapper
ConfigureDefaultMapperConfigures the default mapper when UseCustomMapper method is not used
IsValidValidates Condition, OrderBy, Query, and Mapper; returns a boolean
BuildApplies filtering, ordering, and paging to a queryable context
BuildCompiledCompiles the expressions and returns a delegate for applying filtering, ordering, and paging to an enumerable collection
BuildFilteringExpressionReturns a filtering expression that can be compiled for later use with enumerable collections
BuildEvaluatorReturns an evaluator delegate that can be used to evaluate a queryable context
BuildCompiledEvaluatorReturns a compiled evaluator delegate that can be used to evaluate an enumerable collection
BuildWithPagingApplies filtering, ordering, and paging to a context and returns a paging result
BuildWithPagingCompiledCompiles the expressions and returns a delegate for applying filtering, ordering, and paging to an enumerable collection that returns paging result
BuildWithQueryablePagingApplies filtering, ordering, and paging to a context and returns a queryable paging result
EvaluateDirectly evaluates a context to check if all conditions are valid
csharp
var builder = new QueryBuilder<Person>()
        .AddCondition("name=John")
        .AddOrderBy("age, id");

var query = builder.Build(persons);

AddNestedMapper

The AddNestedMapper method in QueryBuilder<T> allows you to reuse mapper configurations for nested objects when building queries dynamically. This mirrors the functionality available in GridifyMapper<T>, enabling DRY (Don't Repeat Yourself) mapper composition in query builders.

Related Documentation

For detailed information about nested mappers and their benefits, see the AddNestedMapper documentation in GridifyMapper guide.

Basic Usage Examples

Example 1: Reusing Address Mapper Without Prefix

csharp
// Define a reusable address mapper
var addressMapper = new GridifyMapper<Address>()
    .AddMap("city", x => x.City)
    .AddMap("country", x => x.Country);

// Use in QueryBuilder - merges directly
var builder = new QueryBuilder<User>()
    .AddMap("email", x => x.Email)
    .AddNestedMapper(x => x.Address, addressMapper)
    .AddCondition("city=London")
    .AddOrderBy("email");

var result = builder.Build(users.AsQueryable());
// Supports: "city=London", "country=UK"

Example 2: Reusing Address Mapper With Prefix

csharp
// Define a reusable address mapper
var addressMapper = new GridifyMapper<Address>()
    .AddMap("city", x => x.City)
    .AddMap("country", x => x.Country);

// Use in QueryBuilder - with custom prefix
var builder = new QueryBuilder<Company>()
    .AddMap("name", x => x.Name)
    .AddNestedMapper("location", x => x.Address, addressMapper)
    .AddCondition("location.city=Berlin")
    .ConfigurePaging(0, 10);

var result = builder.Build(companies.AsQueryable());
// Supports: "location.city=Berlin", "location.country=Germany"

Example 3: Custom Mapper Classes

csharp
// Define a custom mapper class
public class AddressMapper : GridifyMapper<Address>
{
    public AddressMapper()
    {
        AddMap("city", q => q.City);
        AddMap("country", q => q.Country);
    }
}

// Use with QueryBuilder - without prefix
var builder = new QueryBuilder<User>()
    .AddMap("email", x => x.Email)
    .AddNestedMapper<AddressMapper>(x => x.Address);

// Use with QueryBuilder - with prefix
var builder = new QueryBuilder<Company>()
    .AddMap("name", x => x.Name)
    .AddNestedMapper<AddressMapper>("location", x => x.Address);

Example 4: Multiple Nested Mappers

csharp
var addressMapper = new GridifyMapper<Address>()
    .AddMap("city", x => x.City)
    .AddMap("country", x => x.Country);

var contactMapper = new GridifyMapper<Contact>()
    .AddMap("phone", x => x.Phone)
    .AddMap("email", x => x.Email);

var builder = new QueryBuilder<User>()
    .AddMap("name", x => x.Name)
    .AddNestedMapper("addr", x => x.Address, addressMapper)
    .AddNestedMapper("contact", x => x.Contact, contactMapper)
    .AddCondition("addr.city=London,contact.phone=1234567890")
    .AddOrderBy("name");

var result = builder.Build(users.AsQueryable());