public static IQueryable <T> ApplySort <T>(this IQueryable <T> source, string orderBy, Dictionary <string, PropertyMappingValue> mappingDictionary) { string orderByString = ""; if (source == null) { throw new ArgumentNullException(nameof(source)); } if (mappingDictionary == null) { throw new ArgumentNullException(nameof(mappingDictionary)); } if (string.IsNullOrWhiteSpace(orderBy)) { return(source); } string[] orderBySplit = orderBy.Split(","); foreach (string orderByClause in orderBySplit) { string trimmedOrderBy = orderByClause.Trim(); bool orderDesc = trimmedOrderBy.EndsWith(" desc"); int indexOfSpace = trimmedOrderBy.IndexOf(" "); string propertyName = indexOfSpace == -1 ? trimmedOrderBy : trimmedOrderBy.Remove(indexOfSpace); if (!mappingDictionary.ContainsKey(propertyName)) { throw new ArgumentException($"Mapping doesn't exist for {propertyName}"); } PropertyMappingValue propertyMappingValue = mappingDictionary[propertyName]; if (propertyMappingValue == null) { throw new ArgumentNullException(nameof(propertyMappingValue)); } foreach (string destination in propertyMappingValue.DestinationProperties.Reverse()) { if (propertyMappingValue.Revert) { orderDesc = !orderDesc; } orderByString = orderByString + (!string.IsNullOrWhiteSpace(orderByString) ? "," : "") + destination + (orderDesc ? " descending" : " ascending"); } } return(source.OrderBy(orderByString)); }
private void SetupMappingService() { mappingService = new Mock <IPropertyMappingService>(); var propertyMappingValue = new PropertyMappingValue(new List <string>() { "Created" }, true); var dictionary = new Dictionary <string, PropertyMappingValue> { { "Date", propertyMappingValue } }; mappingService.Setup(m => m.GetPropertyMapping <IPostOutputDto, Post>()).Returns(dictionary); }
private static void OrderSourcePerOrderByClause <T>(ref IQueryable <T> source, ref bool isOrderDescending, PropertyMappingValue propertyMappingValue) { foreach (var destinationProperty in propertyMappingValue.DestinationProperties.Reverse()) { if (propertyMappingValue.Revert) { isOrderDescending = !isOrderDescending; } source = source.OrderBy(destinationProperty + (isOrderDescending ? " descending" : " ascending")); } }
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); } // the orderBy string is separated by ",", so we split it. string[] orderByAfterSplit = orderBy.Split(','); // apply each orderby clause in reverse order - otherwise, the IQueryable will be ordered in the wrong order foreach (string orderByClause in orderByAfterSplit.Reverse()) { // trim the orderByClause, as it might contain leading or trailing spaces. Can't trim the var in foreach, // so use another var. string trimmedOrderByClause = orderByClause.Trim(); // if the sort option ends with with " desc", we order descending, otherwise ascending bool orderDescending = trimmedOrderByClause.EndsWith(" desc"); // remove " asc" or " desc" from the orderByClause, so we get the property name to look for in the // mapping dictionary int indexOfFirstSpace = trimmedOrderByClause.IndexOf(" ", StringComparison.Ordinal); string propertyName = indexOfFirstSpace == -1 ? trimmedOrderByClause : trimmedOrderByClause.Remove(indexOfFirstSpace); // find the matching property if (!mappingDictionary.ContainsKey(propertyName)) { throw new ArgumentException($"Key mapping for {propertyName} is missing"); } // get the PropertyMappingValue PropertyMappingValue propertyMappingValue = mappingDictionary[propertyName]; if (propertyMappingValue == null) { throw new ArgumentNullException(nameof(propertyMappingValue)); } // Run through the property names in reverse so the orderby clauses are applied in the correct order foreach (string destinationProperty in propertyMappingValue.DestinationProperties.Reverse()) { // revert sort order if necessary if (propertyMappingValue.Revert) { orderDescending = !orderDescending; } source = source.OrderBy(destinationProperty + (orderDescending ? " descending" : " ascending")); } } return(source); }