In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.