简体   繁体   中英

Whats wrong with my switch statement?

I just added a sort feature to this line of code via a switch statement, yet the switch statement does not seem to do anything... Is there something I missed? I am a bit of a newbie so I could be wrong and my error could exist somewhere else in my code, so bear with me. Also some suggestions on ways to make this line of code more efficient would be welcome too. Thank you.

        public ProductGetAllActiveResponse GetAllActiveProducts(ProductGetActiveRequest request)
    {
        IEnumerable<SalesOrderHeader> salesOrderMatches = DataModel.SalesOrderHeaders
                                                                   .Where(s => s.CustomerID == request.CustomerId &&
                                                                               s.OrderDate > request.Startdate &&
                                                                               s.OrderDate < request.EndDate);


        ProductGetAllActiveResponse response = new ProductGetAllActiveResponse();

        List<ProductActiveResponse> products = new List<ProductActiveResponse>();     

        foreach (var item in salesOrderMatches)
        {
            foreach (var item2 in item.SalesOrderDetails)
            {
                var product = DataModel.Products.FirstOrDefault(n => n.ProductID == item2.ProductID);
                ProductActiveResponse newResponse = new ProductActiveResponse();
                newResponse.Color = product.Color;
                newResponse.DiscontinuedDate = product.DiscontinuedDate;
                newResponse.ListPrice = product.ListPrice;
                newResponse.Name = product.Name;
                newResponse.OrderDate = item.OrderDate;
                newResponse.ProductId = product.ProductID;
                newResponse.ProductLine = product.ProductLine;
                newResponse.ProductModelId = product.ProductModelID;
                newResponse.ProductNumber = product.ProductNumber;
                newResponse.Style = product.Style;
                products.Add(newResponse);
            }
        }

        response.IndexSize = products.Count();

        IEnumerable<ProductActiveResponse> test;

        switch(request.SortMember)
        {
            case "ProductId":
                if (request.SortDirection == 1)
                   test = products.OrderBy(m => m.ProductId);
                else
                   test = products.OrderByDescending(m => m.ProductId);
                break;
            case "ListPrice":
                if (request.SortDirection == 1)
                    test = products.OrderBy(m => m.ListPrice);
                else
                    test = products.OrderByDescending(m => m.ListPrice);
                break;
            case "Color":
                if (request.SortDirection == 1)
                   test =  products.OrderBy(m => m.Color);
                else
                    test = products.OrderByDescending(m => m.Color);
                break;
            case "OrderDate":
                if (request.SortDirection == 1)
                    test = products.OrderBy(m => m.OrderDate);
                else
                    test = products.OrderByDescending(m => m.OrderDate);
                break;
            default:
                   test =  products.OrderBy(m => m.OrderDate);
                break;
        }


        var trimmedProducts = test.Skip((request.Page - 1) * request.ProductsPerPage).Take(request.ProductsPerPage);

        foreach (var item in trimmedProducts)
        {
            response.ActiveProducts.Add(item);
        }

        if (response.ActiveProducts.Count() == 0)
            response.Success = false;
        else
            response.Success = true;

        return response;
    }

OrderBy and OrderByDescending don't modify the existing collection. They return a new sorted IEnumerable, which is discarded because it isn't assigned to anything.

You could do something like this:

IEnumerable<ProductActiveResponse> sortedProducts;

...

sortedProducts = product.OrderBy(m => m.OrderDate);

...

var trimmedProducts = sortedProducts.Skip((request.Page - 1) * request.ProductsPerPage).Take(request.ProductsPerPage);

The problem is not the switch statement, but your use of OrderBy and not assigning the result to a variable.

OrderBy and OrderByDescending return an IEnumerable<T> collection.

Change

products.OrderBy(m => m.OrderDate);

to

products = products.OrderBy(m => m.OrderDate);

And it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM