简体   繁体   English

使用EntityFramework在Asp.net Mvc中排序

[英]Sorting in Asp.net Mvc using entityframework

 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. 我是Asp.net Mvc的新手。 I have found an error in my switch statement that code is unreachable and also for 'employees'. 我在switch语句中发现一个错误,指出代码不可访问,也适用于“雇员”。 What should I do to fix that error? 我该怎么做才能解决该错误? It's not working for sorting employee name and also for Employee city. 它不适用于排序员工姓名和员工城市。

You are switching on sortby; 您正在打开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? 如果您确定可能的值,则可以调用sortby.ToLower()来小写条件的评估,以确保不会引起错误? Also, 2 of the case statements are missing a break; 另外,其中两个case语句缺少中断; 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;

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

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