Extensions

The DynamicWhere.ex library includes a set of extension methods that enhance the functionality of LINQ queries for dynamic querying purposes

Where On Condition

Where<T>(this IQueryable<T> query, Condition condition)

This extension method allows you to filter an IQueryable based on a single Condition.

Usage:

// Assuming you have an IQueryable<T> query and a Condition condition defined
var filteredQuery = query.Where(condition);

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to filter.

    • condition (Condition): The single condition specifying the filtering criteria.

  • Return Value:

    • IQueryable<T>: A new queryable instance with the specified condition applied.


Where On Group

Where<T>(this IQueryable<T> query, ConditionGroup group)

This extension method allows you to filter an IQueryable based on a specified ConditionGroup.

Usage:

// Assuming you have an IQueryable<T> query and a ConditionGroup conditionGroup defined
var filteredQuery = query.Where(conditionGroup);

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to filter.

    • conditionGroup (ConditionGroup): The condition group specifying the filtering conditions.

  • Return Value:

    • IQueryable<T>: A new queryable instance with the specified conditions applied.

This extension method empowers you to apply complex filtering on your queries, providing flexibility and precision in your data retrieval.


Specify Select

Select<T>(this IQueryable query, List fields)

This extension method enables you to select specific columns from an IQueryable source based on a list of field names. It provides fine-grained control over the data you retrieve, optimizing query performance and reducing unnecessary data transfer.

Usage:

// Assuming you have an IQueryable<T> query and a List<string> fields defined
var selectedQuery = query.Select(fields);

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to select columns from.

    • fields (List): A list of field names to include in the query result.

  • Return Value:

    • IQueryable<T>: A new queryable instance with the specified columns selected.


Order By One

Order<T>(this IQueryable<T> query, OrderBy order)

This extension method allows you to order the results of an IQueryable based on a specified OrderBy configuration.

Usage:

// Assuming you have an IQueryable<T> query and an OrderBy order defined
var orderedQuery = query.Order(order);

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to order.

    • order (OrderBy): The order-by configuration specifying the field and sorting direction.

  • Return Value:

    • IQueryable<T>: A new queryable instance with the specified ordering applied.


Order By Many

Order<T>(this IQueryable<T> query, List<OrderBy> orders)

This extension method allows you to order the results of an IQueryable based on a list of OrderBy configurations.

Usage:

// Assuming you have an IQueryable<T> query and a List<OrderBy> orders defined
var orderedQuery = query.Order(orders);

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to order.

    • orders (List): A list of order-by configurations specifying the fields and sorting directions.

  • Return Value:

    • IQueryable<T>: A new queryable instance with the specified ordering applied.


Pagination

Page<T>(this IQueryable<T> query, PageBy page)

This extension method allows you to apply pagination to an IQueryable, specifying the page number and page size.

Usage:

// Assuming you have an IQueryable<T> query and a PageBy page defined
var pagedQuery = query.Page(page);

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to apply pagination to.

    • page (PageBy): The page object specifying the page number and page size.

  • Return Value:

    • IQueryable<T>: A new queryable instance with pagination applied.


Apply Filter

Filter<T>(this IQueryable<T> query, Filter filter)

This extension method allows you to filter an IQueryable based on a Filter configuration, including conditions, sorting, and pagination.

Usage:

// Assuming you have an IQueryable<T> query and a Filter filter defined
var filteredQuery = query.Filter(filter);

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to filter.

    • filter (Filter): The filter configuration specifying conditions, sorting, and pagination.

  • Return Value:

    • IQueryable<T>: A new queryable instance with the specified filtering, ordering, and pagination applied.


Get Filter Result

ToListAsync<T>(this IQueryable<T> query, Filter filter)

This extension method allows you to execute an asynchronous query and retrieve the results as a list based on the specified group of conditions inside filter object.

Usage:

FilterResult<T> result = await query.ToListAsync(filter);

// Accessing the data result
List<T> queryResults = result.Data;

int pageNumber = result.PageNumber;
int pageSize = result.PageSize;
int pageCount = result.PageCount;
int totalCount = result.TotalCount;

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to execute the dynamic query on.

    • filter(Filter): The filter object containing group of conditions for dynamic querying. It may also include optional list of order-by criteria and pagination settings.

  • Return Value:

    • FilterResult<T>: An object representing the query results. It includes pagination information (if specified) and a list of entities that match the group of conditions specified in the filter.


Get Segment Result

ToListAsync<T>(this IQueryable<T> query, Segment segment)

This extension method allows you to execute an asynchronous query and retrieve the results as a list based on the specified segment of conditions.

Usage:

// Previous usage v1.4.3 and less
List<T> results = await query.ToListAsync(segment);

// Updated usage v1.5.0 
SegmentResult<T> result = await query.ToListAsync(segment);

// Accessing the data result
List<T> queryResults = result.Data;

int pageNumber = result.PageNumber;
int pageSize = result.PageSize;
int pageCount = result.PageCount;
int totalCount = result.TotalCount;

Documentation:

  • Parameters:

    • query (IQueryable): The queryable source to execute the dynamic query on.

    • segment (Segment): The segment object containing sets of conditions for dynamic querying. It may also include optional list of order-by criteria and pagination settings.

  • Return Value:

    • SegmentResult<T>: An object representing the query results. It includes pagination information (if specified) and a list of entities that match the conditions specified in the segment.

Last updated