1
 ViewBag.ParameterByName = string.IsNullOrEmpty(sortBy) ? "Name desc" : "";
 ViewBag.ParameterByCity = sortBy == "emp_City" ? "desc City" : "emp_City";
            var employees = db.Employees.AsQueryable();

switch (sortBy)
            {
                case "Name desc":
                    employees = employees.OrderByDescending(x => x.emp_name);
                    break;
                case "desc City":

                    employees = employees.OrderByDescending(x => x.emp_City);
                case "emp_City":
                    employees= employees.OrderBy(x => x.emp_City);


                default:
                    employees = employees.OrderBy(x => x.emp_name);
                    break;
        }

I am new to Asp.net Mvc. I have found an error in my switch statement that code is unreachable and also for 'employees'. What should I do to fix that error? It's not working for sorting employee name and also for Employee city.

1
  • Actually emp_City is my database column name.And it is same here it in the database.but why i got the switch and employees object as unreachable Commented Jun 13, 2017 at 18:20

1 Answer 1

1

You are switching on sortby; are you sure these are the only possible conditions? If you are sure on the possible values, maybe calling sortby.ToLower() to lower-case the evaluation of the conditions, just to be sure that isn't causing the error? Also, 2 of the case statements are missing a break; at the end:

case "desc City":
    employees = employees.OrderByDescending(x => x.emp_City);
    break;
case "emp_City":
    employees= employees.OrderBy(x => x.emp_City);
    break;
Sign up to request clarification or add additional context in comments.

Comments

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.