public void WhenFilterContainSortDescriptionWithDirectionThenCreatesMatchingSortDescription() { const string Orderstring = "IntValue desc"; var descriptions = _factory.Create <FakeItem>(Orderstring); var filter = new ModelFilter <FakeItem>(x => true, null, descriptions, 0, -1, false); var sortedItems = filter.Filter(_items) .ToArray(); Assert.AreEqual( 3, sortedItems.OfType <FakeItem>() .ElementAt(0) .IntValue); Assert.AreEqual( 2, sortedItems.OfType <FakeItem>() .ElementAt(1) .IntValue); Assert.AreEqual( 1, sortedItems.OfType <FakeItem>() .ElementAt(2) .IntValue); }
/// <summary> /// To the query options. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="odataQueryOptions">The o data query options.</param> /// <returns>QueryOptions{``0}.</returns> public static QueryOptions <T> ToQueryOptions <T>(this ODataQueryOptions <T> odataQueryOptions) { if (odataQueryOptions == null) { return(null); } var nameResolver = new MemberNameResolver(); var selectFactory = new SelectExpressionFactory <T>(nameResolver, new RuntimeTypeProvider(nameResolver)); var filterFactiory = new FilterExpressionFactory(nameResolver, Enumerable.Empty <IValueExpressionFactory>()); var orderFactory = new SortExpressionFactory(nameResolver); return(new QueryOptions <T> { Top = odataQueryOptions.Top?.Value, Skip = odataQueryOptions.Skip?.Value, Select = !string.IsNullOrEmpty(odataQueryOptions.SelectExpand?.RawSelect) ? selectFactory.Create(odataQueryOptions.SelectExpand.RawSelect) : null, Expand = !string.IsNullOrEmpty(odataQueryOptions.SelectExpand?.RawExpand) ? odataQueryOptions.SelectExpand.RawExpand.Split(',').ToList() : null, OrderBy = !string.IsNullOrEmpty(odataQueryOptions.OrderBy?.RawValue) ? orderFactory.Create <T>(odataQueryOptions.OrderBy.RawValue) .Select( description => new SortDescription( description.KeySelector, (SortDirection)description.Direction)) .ToList() : null, Filter = !string.IsNullOrEmpty(odataQueryOptions.Filter?.RawValue) ? filterFactiory.Create <T>(odataQueryOptions.Filter.RawValue) : null }); }