如何使用实体框架.Net Core通过{ASC和DESC}使用订单

时间:2019-04-23 12:11:16

标签: c# entity-framework .net-core

我正在使用角度数据表显示数据列表并执行服务器端排序。当我对任何列进行排序时,我会收到列的名称和排序顺序。我正在使用以下代码对记录进行排序。

if (!string.IsNullOrEmpty(sortField) && !string.IsNullOrEmpty(sortOrder))
            {
                if (sortOrder == "asc")
                {
                    if (sortField == "cnic")
                    {
                        data2 = data2.OrderBy(a => a.Cnic);
                    }
                    else if (sortField == "firstName")
                    {
                        data2 = data2.OrderBy(a => a.FirstName);
                    }
                    else if (sortField == "fatherName")
                    {
                        data2 = data2.OrderBy(a => a.FatherName);
                    }
                    else if (sortField == "gender")
                    {
                        data2 = data2.OrderBy(a => a.Gender);
                    }
                    else if (sortField == "designation")
                    {
                        data2 = data2.OrderBy(a => a.Designation);
                    }
                    else if (sortField == "cadreName")
                    {
                        data2 = data2.OrderBy(a => a.CadreName);
                    }
                    else if (sortField == "departmentName")
                    {
                        data2 = data2.OrderBy(a => a.DepartmentName);
                    }
                    else if (sortField == "districtName")
                    {
                        data2 = data2.OrderBy(a => a.DistrictName);
                    }
                    else if (sortField == "placeOfPostinfName")
                    {
                        data2 = data2.OrderBy(a => a.PlaceOfPostingName);
                    }
                    else if (sortField == "bps")
                    {
                        data2 = data2.OrderBy(a => a.Bps);
                    }
                    else if (sortField == "seniorityNo")
                    {
                        data2 = data2.OrderBy(a => a.SeniorityNo);
                    }
                }
                else
                {
                    if (sortField == "cnic")
                    {
                        data2 = data2.OrderByDescending(a => a.Cnic);
                    }
                    else if (sortField == "firstName")
                    {
                        data2 = data2.OrderByDescending(a => a.FirstName);
                    }
                    else if (sortField == "fatherName")
                    {
                        data2 = data2.OrderByDescending(a => a.FatherName);
                    }
                    else if (sortField == "gender")
                    {
                        data2 = data2.OrderByDescending(a => a.Gender);
                    }
                    else if (sortField == "designation")
                    {
                        data2 = data2.OrderByDescending(a => a.Designation);
                    }
                    else if (sortField == "cadreName")
                    {
                        data2 = data2.OrderByDescending(a => a.CadreName);
                    }
                    else if (sortField == "departmentName")
                    {
                        data2 = data2.OrderByDescending(a => a.DepartmentName);
                    }
                    else if (sortField == "districtName")
                    {
                        data2 = data2.OrderByDescending(a => a.DistrictName);
                    }
                    else if (sortField == "placeOfPostinfName")
                    {
                        data2 = data2.OrderByDescending(a => a.PlaceOfPostingName);
                    }
                    else if (sortField == "bps")
                    {
                        data2 = data2.OrderByDescending(a => a.Bps);
                    }
                    else if (sortField == "seniorityNo")
                    {
                        data2 = data2.OrderByDescending(a => a.SeniorityNo);
                    }
                }
            }

是否有任何简单的方法来执行此任务...我期望的代码是,我只想输入顺序列名称(cnic,firstname,fathername ...)和排序顺序(asc,desc)。我要对许多列和数据表进行排序,为此我需要一种通用方法。

0 个答案:

没有答案