Skip to content

Instantly share code, notes, and snippets.

@ShawnShiSS
Created November 21, 2020 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnShiSS/7cc6e975ac55bdaff7124191fae505ea to your computer and use it in GitHub Desktop.
Save ShawnShiSS/7cc6e975ac55bdaff7124191fae505ea to your computer and use it in GitHub Desktop.
Example specification for searching entities with support for pagination, sorting for datatables.
namespace CleanArchitectureCosmosDB.Core.Specifications
{
public class ToDoItemSearchSpecification : Specification<Entities.ToDoItem>
{
public ToDoItemSearchSpecification(string title = "",
int pageStart = 0,
int pageSize = 50,
string sortColumn = "title",
SortDirection sortDirection = SortDirection.Ascending
)
{
if (!string.IsNullOrWhiteSpace(title))
{
Query.Where(item => item.Title.ToLower().Contains(title.ToLower()));
}
// Pagination
if (pageSize != -1) //Display all entries and disable pagination
{
Query.Skip(pageStart).Take(pageSize);
}
// Sort
switch (sortColumn.ToLower())
{
case ("title"):
{
if (sortDirection == SortDirection.Ascending)
{
Query.OrderBy(x => x.Title);
}
else
{
Query.OrderByDescending(x => x.Title);
}
}
break;
default:
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment