.net 6 how to implement query sorting in developing TodoList applications
This article mainly explains ".NET 6 development TodoList application how to achieve query sorting", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn ".NET 6 development TodoList application how to achieve query sorting" bar!
Demand
Another requirement for the query is to sort the results according to the sort field requested by the front end.
target
The sorted results are returned according to the sorting requirements.
Principles and ideas
It can be relatively simple to sort according to the request of the front end, combined with the Specification we have written before.
Realize
Let's use the TodoItem request as an example and add a sort field to the query request:
GetTodoItemsWithConditionQuery.cs
Using AutoMapper;using AutoMapper.QueryableExtensions;using MediatR;using TodoList.Application.Common.Interfaces;using TodoList.Application.Common.Mappings;using TodoList.Application.Common.Models;using TodoList.Application.TodoItems.Specs;using TodoList.Domain.Entities;using TodoList.Domain.Enums;namespace TodoList.Application.TodoItems.Queries.GetTodoItems;public class GetTodoItemsWithConditionQuery: IRequest {public Guid ListId {get; set;} public bool? Done {get; set;} public string? Title {get; set;} public PriorityLevel? PriorityLevel {get; set;} public string? SortOrder {get; set;} = "title_asc"; public int PageNumber {get; set;} = 1; public int PageSize {get; set;} = 10;} public class GetTodoItemsWithConditionQueryHandler: IRequestHandler {private readonly IRepository _ repository; private readonly IMapper _ mapper; public GetTodoItemsWithConditionQueryHandler (IRepository repository, IMapper mapper) {_ repository = repository; _ mapper = mapper;} public async Task Handle (GetTodoItemsWithConditionQuery request, CancellationToken cancellationToken) {var spec = new TodoItemSpec (request) Return await _ repository .GetAsQueryable (spec) .ProjectTo (_ mapper.ConfigurationProvider) .PaginatedListAsync (request.PageNumber, request.PageSize);}}
At the same time, the conditions originally written in the query are integrated into TodoItemSpec:
TodoItemSpec.cs
/ / omit other... public TodoItemSpec (GetTodoItemsWithConditionQuery query): base (x = > x.ListId = = query.ListId & & (! query.Done.HasValue | | x.Done = = query.Done) & & (! query.PriorityLevel.HasValue | | x.Priority = = query.PriorityLevel) & & (string.IsNullOrEmpty (query.Title) | | x.Titleroom.Trim (). Contains (query) .title! .ToLower ()) {if (string.IsNullOrEmpty (query.SortOrder)) return Switch (query.SortOrder) {/ / limited presentation default: ApplyOrderBy (x = > x.Title!); break; case "title_desc": ApplyOrderByDescending (x = > x.Title!); break; case "priority_asc": ApplyOrderBy (x = > x.Priority); break Case "priority_desc": ApplyOrderByDescending (x = > x.Priority); break;}} verification
Start the Api project and execute the request for querying TodoItem:
Request
Response
Thank you for your reading. the above is the content of "how to achieve query sorting in the development of TodoList applications in .NET 6". After the study of this article, I believe you have a deeper understanding of how to achieve query sorting in the development of TodoList applications in .NET 6, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!