public async Task <IEnumerable <ProductEntity> > GetProductsForOrderAsync( Guid orderId, OrderProductSortProperty sortProperty, SortDirection sortDirection, CancellationToken cancellationToken) { using (var context = _dbContextProvider.GetNewDbContext()) { var query = context.Set <OrderProductRelationEntity>() .AsNoTracking() .Where(entity => entity.OrderId == orderId); query = SortProducts(query, sortProperty, sortDirection); var relations = await query.ToArrayAsync(cancellationToken); var products = _mapper.Map <IEnumerable <ProductEntity> >(relations); return(products); } }
private static IQueryable <OrderProductRelationEntity> SortProducts( IQueryable <OrderProductRelationEntity> query, OrderProductSortProperty sortProperty, SortDirection sortDirection) { if (sortProperty == OrderProductSortProperty.Sku) { if (sortDirection == SortDirection.Descending) { return(query.OrderByDescending(entity => entity.ProductSku)); } return(query.OrderBy(entity => entity.ProductSku)); } if (sortDirection == SortDirection.Descending) { return(query.OrderByDescending(entity => entity.ProductName)); } return(query.OrderBy(entity => entity.ProductName)); }