我的switch语句有什么问题?

时间:2014-03-04 17:34:00

标签: c#

我刚刚通过switch语句为这行代码添加了一个排序功能,但是switch语句似乎没有做任何事情......我错过了什么?我是一个新手,所以我可能是错的,我的错误可能存在于我的代码中的其他地方,所以忍受我。此外,一些关于如何使这一代码更高效的建议也会受到欢迎。谢谢。

        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;
    }

2 个答案:

答案 0 :(得分:5)

OrderBy和OrderByDescending不会修改现有集合。它们返回一个新的已排序的IEnumerable,它被丢弃,因为它没有被分配给任何东西。

你可以这样做:

IEnumerable<ProductActiveResponse> sortedProducts;

...

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

...

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

答案 1 :(得分:4)

问题不在于switch语句,而是您使用OrderBy而不是将结果分配给变量。

OrderByOrderByDescending会返回IEnumerable<T>个集合。

更改

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

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

它会起作用。