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.
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);
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;
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 );
}
Note that you are not testing against your sorted list (tmpList) but rather against the contents of objDB_ArjunEntities.Employees.ToList ( ). Was that intended?