Home > Back-end >  Problem in implementing order by logic for multiple condition to order the final results
Problem in implementing order by logic for multiple condition to order the final results

Time:10-28

I have a grid where I am showing a list of Agents for a particular department. These agents are real estate agents who post Listings for House rentals.

Grid shows basic information like AgentName, NumberofActiveListings, NumberofSoldAndExpiredListings etc..

Now the requirement is by default the list in the grid should be sorted based on active listings in descending order(NumberofActiveListings). If multiple agents have the same number of Active Listings then sort list of agents by NumberofSoldAndExpiredListings in descending order. If multiple agents have same number of NumberofActiveListings and NumberofSoldAndExpiredListings then agent list should be sorted by Name in Ascending order.

Also, user can click on an individual columns on-grid and data will be sorted according to that column.

Below is the DTO class which holds the final results :

     public class AgentResultDto
            {
                public int AgentId { get; set; }
                public string AgentName { get; set; }
                public int NumberofActiveListings { get; set; }
                public int NumberofSoldAndExpiredListings { get; set; }
            }
    
        public class GridviewInput 
        {
            public string SortingColumn { get; set; } //hold the column name user will click on to sort the data
            //other params
        }

public virtual async Task<AgentResultDto> GetAgents(GridviewInput model)
  {
     List<AgentResultDto> agents = new List<AgentResultDto>();
     //logic to populate agent list
     
     agents = agents.OrderBy(model.SortingColumn).ToList(); 
  }

but here I am confused with how do I specify condition like that if NumberofActiveListings is same then order by NumberofSoldAndExpiredListings and if NumberofSoldAndExpiredListings is same then order by AgentName ascending.

Can anyone please guide me to implement this requirement with order by logic?

CodePudding user response:

As for sorting by multiple columns, take a look at the .ThenBy() and .ThenByDescending() functions that work in conjunction with the initial .OrderBy() and .OrderByDescending().

As for dynamically selecting the columns to be sorted, take a look at Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>.

CodePudding user response:

there are 2 main ways

  1. Using reflections (slow if the list is big)
 agents =  agents.OrderBy(o => o.GetType()
                    .GetProperty(model.SortingColumn)
                    .GetValue(o, null)).ToList();
  1. this is faster but you neeed to use the special function
 var columnGetter = CreateGetter(quotes[0].GetType(), model.SortingColumn); 
 quotes = quotes .OrderBy(o => columnGetter(o)).ToList();


public static Func<object, object> CreateGetter(Type runtimeType, string propertyName)
{
    var propertyInfo = runtimeType.GetProperty(propertyName);

    var obj = Expression.Parameter(typeof(object), "obj");

    var objT = Expression.TypeAs(obj, runtimeType);

    var property = Expression.Property(objT, propertyInfo);

    var convert = Expression.TypeAs(property, typeof(object));

    return (Func<object, object>)Expression.Lambda(convert, obj).Compile();
}
  • Related