lwl1569  

教程的方法:(orderby多个参数的数据解析和排序放到一起,参数传递直接传string)
0. 参数传递(从URL到代码查询):参考解析URL中的批量信息中最后的方法

    public class TouristRouteResourceParamaters
    {
        public string OrderBy { get; set; }
    }
  1. 功能实现
    1.1. 字典映射:参考字典映射
    1.2排序方法实现
    依赖项:System.Linq.Dynamic.Core(linq增强)
using FakeXiecheng.API.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;

namespace XXX
{
    public static class IQueryableExtensions
    {
        public static IQueryable<T> ApplySort<T>(this IQueryable<T> source,string orderBy, Dictionary<string, PropertyMappingValue> mappingDictionary)
        {
        public static IQueryable<T> ApplySort<T>( this IQueryable<T> source, string orderBy, Dictionary<string, PropertyMappingValue> mappingDictionary)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (mappingDictionary == null)
            {
                throw new ArgumentNullException(nameof(mappingDictionary));
            }

            if (string.IsNullOrWhiteSpace(orderBy))
            {
                return source;
            }

            string orderByString = string.Empty;

            string[] orderByAfterSplit = orderBy.Split(',');

            foreach(string order in orderByAfterSplit)
            {
                string trimmedOrder = order.Trim();

                // 通过字符串“ desc”来判断升序还是降序
                bool orderDescending = trimmedOrder.EndsWith(" desc");

                // 删除升序或降序字符串 " asc" or " desc"来获得属性的名称
                int indexOfFirstSpace = trimmedOrder.IndexOf(" ");
                string propertyName = indexOfFirstSpace == -1 ? trimmedOrder : trimmedOrder.Remove(indexOfFirstSpace);

                if (!mappingDictionary.ContainsKey(propertyName))
                {
                    throw new ArgumentException($"Key mapping for {propertyName} is missing");
                }

                PropertyMappingValue propertyMappingValue = mappingDictionary[propertyName];
                if (propertyMappingValue == null)
                {
                    throw new ArgumentNullException("propertyMappingValue");
                }

                foreach(string destinationProperty in 
                    propertyMappingValue.DestinationProperties.Reverse())
                {
                    // 给IQueryable 添加排序字符串
                    orderByString = $"{orderByString}{(string.IsNullOrWhiteSpace(orderByString) ? string.Empty : ", ")}{destinationProperty}{(orderDescending ? " descending" : " ascending")}";
                }
            }
            return source.OrderBy(orderByString);
        }
    }
}

使用:
1、调用服务

        private readonly IPropertyMappingService _propertyMappingService;

        public TouristRouteRepository(IPropertyMappingService propertyMappingService)
        {
            _propertyMappingService = propertyMappingService;
        }

2、调用方法:(已知result是IQueryabel,orderby是排序字符串,例如“title,description desc,id”)

Dictionary<string, PropertyMappingValue> touristRouteMappingDictionary = _propertyMappingService.GetPropertyMapping<TouristRouteDto, TouristRoute>();
result = result.ApplySort(orderBy, touristRouteMappingDictionary);
posted on 2021-09-28 16:49  lwl1569  阅读(105)  评论(0编辑  收藏  举报