如何对EDMX类列表进行排序 [英] How to sort a EDMX class list

查看:79
本文介绍了如何对EDMX类列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试学习MVC。我有一个EDMX类,它代表一个Employee实体,它有5个属性ID,Name,Email,Address& IsActive。



我正在尝试使用linq对员工列表进行排序。排序字段是根据列表页面中的哪一列动态设置的。



这是我写的代码



Hi,

I am trying to study MVC. I have an EDMX class which represents an Employee entity which has 5 properties ID,Name,Email,Address & IsActive.

I am trying to sort the employee list using linq. The sorting field is dynamically set based on which column is clicked in list page.

This is the code i have written

public List<Employee> GetEmployees(bool includeInActive = false, string searchKey = "", string sortingField = "ID", string sortingDirection = "asc")
       {
           DB_Arjun objDB_ArjunEntities;
           try
           {
               objDB_ArjunEntities = new DB_Arjun();
               List<Employee> lstEmployee = new List<Employee>();

               if (string.IsNullOrWhiteSpace(searchKey))
               {
                   if (sortingDirection != "asc")
                   {
                       var tmpList = from employee in objDB_ArjunEntities.Employees.ToList()
                                     orderby sortingField descending
                                     select employee;

                       foreach (Employee emp in tmpList.ToList())
                       {
                           if (includeInActive)
                               lstEmployee.Add(emp);
                           else if (!includeInActive && emp.IsActive == true)
                               lstEmployee.Add(emp);
                       }
                   }
                   else
                   {
                       var tmpList = from employee in objDB_ArjunEntities.Employees.ToList()
                                     orderby sortingField
                                     select employee;

                       foreach (Employee emp in tmpList.ToList())
                       {
                           if (includeInActive)
                               lstEmployee.Add(emp);
                           else if (!includeInActive && emp.IsActive == true)
                               lstEmployee.Add(emp);
                       }
                   }
               }
               else
               {
                   var tmpList = from employee in objDB_ArjunEntities.Employees.ToList()
                                 where employee.Name == searchKey || employee.Address == searchKey || employee.Email == searchKey
                                 orderby sortingField
                                 select employee;

                   //var query =  from employee in objDB_ArjunEntities.Employees.ToList().Where(objDB_ArjunEntities.Employees. == searchKey || objDB_ArjunEntities.Employees. == searchKey || employee.Email == searchKey).OrderBy("Name asc");

                   foreach (Employee emp in objDB_ArjunEntities.Employees.ToList())
                   {
                       if (includeInActive && (emp.Name.Contains(searchKey) || emp.Address.Contains(searchKey) || emp.Email.Contains(searchKey)))
                           lstEmployee.Add(emp);
                       else if (!includeInActive && emp.IsActive == true && (emp.Name.Contains(searchKey) || emp.Address.Contains(searchKey)))
                           lstEmployee.Add(emp);
                   }
               }
               return lstEmployee;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }









但问题是排序不能正常工作。排序地址和sortdirection是正确的,但排序不起作用..





任何人都可以帮助我





But the problem is that the sorting is not working properly. The sorting address and sortdirection is correct but the sorting is not working..


Can anyone please help me

推荐答案



您不必执行升序或降序排序。只需执行升序排序并从一端或另一端访问结果列表。


You do not have to perform an ascending or descending sort. Just perform an ascending sort and access the resulting list from one end or the other.



添加.ToList()到Linq语句的末尾,您不需要稍后添加它。例如,


By adding .ToList ( ) to the end of your Linq statements, you do not need to add it later on. For example,

temporary =
    ( from employee in objDB_ArjunEntities.
                           Employees.ToList ( )
      orderby sortingField
      select employee ).ToList ( );



其他如果您的代码:


In else if of your code:

if (includeInActive)
    lstEmployee.Add(emp);
else if (!includeInActive && emp.IsActive == true)
    lstEmployee.Add(emp);



您无需测试!includeInActive - 我们知道这是错误的。另外,总是在if语句体周围放置大括号;因为emp.IsActive是一个布尔值,你只需要测试它的值。修改后的代码:


you do not have to test !includeInActive - we know it's false. Also, ALWAYS place braces around the if statement bodies; because emp.IsActive is a boolean, you only have to test its values. Revised code:

if ( includeInActive )
    {
    lstEmployee.Add ( emp );
    }
else if ( emp.IsActive )
    {
    lstEmployee.Add ( emp );
    }



如果提供搜索键,排序总是提升。不应该按照调用者的偏好排序顺序吗?


In the case where a search key is provided, the sort is always ascending. Shouldn't sort order follow the invoker's preference?



声明之后


Following the statement

var tmpList = from employee in objDB_ArjunEntities.
                                   Employees.ToList()
              where ( employee.Name == searchKey ||
                      employee.Address == searchKey || e
                      mployee.Email == searchKey )
              orderby sortingField
              select employee;



您无需像在
$ b $中那样重新测试相同的条件b


you do not have to retest the same conditions like you do in

foreach ( Employee emp in objDB_ArjunEntities.Employees.ToList ( ) )
    {
    if ( includeInActive &&
         ( emp.Name.Contains ( searchKey ) ||
           emp.Address.Contains ( searchKey ) ||
           emp.Email.Contains ( searchKey ) ) )
        {
        lstEmployee.Add(emp);
        }
    else if ( !includeInActive &&
              emp.IsActive == true &&
              ( emp.Name.Contains ( searchKey ) ||
                emp.Address.Contains ( searchKey ) ) )
        {
        lstEmployee.Add(emp);
        }
    }



再一次,你所需要的只是


Again, all that you need is

if ( includeInActive )
    {
    lstEmployee.Add ( emp );
    }
else if ( emp.IsActive )
    {
    lstEmployee.Add ( emp );
    }



请注意,您没有针对排序列表(tmpList)进行测试,而是针对objDB_ArjunEntities.Employees.ToList()的内容。这是有意的吗?


Note that you are not testing against your sorted list (tmpList) but rather against the contents of objDB_ArjunEntities.Employees.ToList ( ). Was that intended?


这篇关于如何对EDMX类列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆