1
\$\begingroup\$

I have the List method that takes sorting parameter to sort results. The sorting parameter value is the same as the column name.

public ActionResult List(string sorting = "Name", string sortingMethod = "ASC")
{
    var productList = productRepository.Get();

    if(!String.IsNullOrEmpty(Sorting){
        if(String.IsNullOrEmpty(SortingMethod) ||SortingMethod == "ASC"){
            switch(sorting){
                case "Name" :
                    productList = productList.OrderBy(x => x.Name); break;
                case "Price" :
                    productList = productList.OrderBy(x => x.Price); break;
                case "Category" :
                    productList = productList.OrderBy(x => x.Price); break;
                //...
            }
        }else{
            switch(sorting){
                case "Name" :
                    productList = productList.OrderByDescending(x => x.Name); break;
                case "Price" :
                    productList = productList.OrderByDescending(x => x.Price); break;
                case "Category" :
                    productList = productList.OrderByDescending(x => x.Price); break;
                //...
            }
        }
    }

    return View(productList.ToList());
}

But I believe there is a better way to make code shorter.

public ActionResult List(string sorting = "Name", string sortingMethod = "ASC")
{
    var productList = productRepository.Get();

    if(!String.IsNullOrEmpty(Sorting){
        if(String.IsNullOrEmpty(SortingMethod) ||SortingMethod == "ASC"){
            productList = productList.OrderBy();  // <== ???
        }else{
            productList = productList.OrderByDescending(); // <== ???
        }
    }

    return View(productList.ToList());
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

What about seperating your sorting from your criteria? I'm like you, I'm sure there is another way so am interested to see other opinions. I'm not exactly sure if this would scale for multiple orderBys either but I think it might (NOTE: this code is untested and some types are not defined)

Something along the lines of:

public ActionResult List(string sorting = "Name", string sortingMethod = "ASC")
{
   var sortDirection = GetSortDirection(sortingMethod);
   var criteria = GetSortCriteria(sorting);

   var products = GetProductsOrderBy(criteria, sortDirection);

   return View(products.ToList());
}

// I'm not actually sure of the return type here.  Will attempt to test when I
// get in front of my development pc
Func<Product, object> GetSortCriteria(string sortOn) 
{
   switch(sortOn) 
   {
      case "Price":
      case "Category":
         return x => x.Price;
      default:
         return x.Name;
   }
}

IEnumerable<ProductList> GetProductsOrderBy(Action sortOn, SortDirection sortDirection)
{
    var productList = productRepository.Get();

    return sortDirection == SortDirection.Ascending ?
           productList.OrderBy(sortOn) :
           productList.OrderByDescending(sortOn);
}

SortDirection GetSortDirection(string sortBy) 
{
   return sortBy == "ASC" ? SortDirection.Ascending : SortDirection.Descending;
}
\$\endgroup\$
2
  • \$\begingroup\$ I just tried the code, but it's not working. Error message say : The type arguments for method 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. C:\Users\mark\documents\visual studio 2012\... \$\endgroup\$ Commented May 28, 2013 at 18:26
  • \$\begingroup\$ @Expertwannabe I thought that might be the case but wasn't 100 when typing. I'll have another think and try another option. \$\endgroup\$
    – dreza
    Commented May 28, 2013 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.