網(wǎng)站首頁 編程語言 正文
.NET?6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)查詢排序_實(shí)用技巧
作者:CODE4NOTHING ? 更新時(shí)間: 2022-03-22 編程語言需求
關(guān)于查詢的另一個(gè)需求是要根據(jù)前端請求的排序字段進(jìn)行對結(jié)果相應(yīng)的排序。
目標(biāo)
實(shí)現(xiàn)根據(jù)排序要求返回排序后的結(jié)果
原理與思路
要實(shí)現(xiàn)根據(jù)前端請求的進(jìn)行相應(yīng)排序,結(jié)合我們之前寫好的Specification
,可以比較簡單地做到。
實(shí)現(xiàn)
我們還是用TodoItem
請求來舉例,再添加一個(gè)排序字段到查詢請求中:
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<PaginatedList<TodoItemDto>> { 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<GetTodoItemsWithConditionQuery, PaginatedList<TodoItemDto>> { private readonly IRepository<TodoItem> _repository; private readonly IMapper _mapper; public GetTodoItemsWithConditionQueryHandler(IRepository<TodoItem> repository, IMapper mapper) { _repository = repository; _mapper = mapper; } public async Task<PaginatedList<TodoItemDto>> Handle(GetTodoItemsWithConditionQuery request, CancellationToken cancellationToken) { var spec = new TodoItemSpec(request); return await _repository .GetAsQueryable(spec) .ProjectTo<TodoItemDto>(_mapper.ConfigurationProvider) .PaginatedListAsync(request.PageNumber, request.PageSize); } }
同時(shí)把原本寫在查詢中的條件整合到了TodoItemSpec
中:
TodoItemSpec.cs
// 省略其他... 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.Title!.Trim().ToLower().Contains(query.Title!.ToLower()))) { if (string.IsNullOrEmpty(query.SortOrder)) return; switch (query.SortOrder) { // 僅作有限的演示 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; } }
驗(yàn)證
啟動(dòng)Api項(xiàng)目,執(zhí)行查詢TodoItem的請求:
請求
響應(yīng)
總結(jié)
這樣我們就完成了根據(jù)前端需求進(jìn)行后端排序并返回結(jié)果的需求,下一篇文章我們將介紹查詢中的最后一個(gè)不是很常用,但是在某些情況下很有用的概念:數(shù)據(jù)塑形。
原文鏈接:https://www.cnblogs.com/code4nothing/p/15751036.html
相關(guān)推薦
- 2022-06-01 Python使用xlrd和xlwt批量讀寫excel文件的示例代碼_python
- 2022-05-20 C++實(shí)現(xiàn)簡單學(xué)生成績管理系統(tǒng)_C 語言
- 2022-04-11 python進(jìn)程結(jié)束后端口占用問題解析_python
- 2022-08-28 配置zabbix監(jiān)控nginx狀態(tài),監(jiān)控華為路由器
- 2022-05-14 linq中的分區(qū)操作符_實(shí)用技巧
- 2022-08-31 C++中關(guān)于this指針的入門介紹_C 語言
- 2022-05-05 python?scipy.spatial.distance?距離計(jì)算函數(shù)??_python
- 2022-02-18 解決 Syntax Error: Error: PostCSS received undefined
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支