简体   繁体   English

我的switch语句怎么了?

[英]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? 我只是通过switch语句向该代码行添加了排序功能,但switch语句似乎没有任何作用...我错过了什么吗? 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. OrderBy和OrderByDescending不会修改现有集合。 They return a new sorted IEnumerable, which is discarded because it isn't assigned to anything. 它们返回一个新排序的IEnumerable,由于未分配任何内容,因此将其丢弃。

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. 问题不在于switch语句,而是您对OrderBy的使用,而不是将结果分配给变量。

OrderBy and OrderByDescending return an IEnumerable<T> collection. OrderByOrderByDescending返回IEnumerable<T>集合。

Change 更改

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

to

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

And it will work. 它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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