Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
I have one generic function like:
C#
public TabMasterListViewModel GetTabMasterList(string OrderByColumn, string OrderType, int PageSize, int CurrentPage)
        {
            try
            {
                if ((CurrentPage == 0) || (PageSize == 0))
                    return null;

                IQueryable<TabMaster> query = _tabmasterRepository.GetQueryable();
                TabMasterListViewModel model = new TabMasterListViewModel();
                model.TotalItemCount = query.Count();
                if (model.TotalItemCount == 0)
                    return null;
                model.TotalPageCount = (int)Math.Ceiling((double)model.TotalItemCount / (double)PageSize);
                if (model.TotalPageCount != 1)
                {
                    if (OrderType.ToUpper() == "ASC")
                        query = query.OrderBy(x => x.colID).Skip((CurrentPage - 1) * PageSize).Take(PageSize);
                    else
                        query = query.OrderByDescending(x => x.colID).Skip((CurrentPage - 1) * PageSize).Take(PageSize);
                }
                model.ThisPageItemCount = query.Count();
                model.TabMasterList = new List<TabMasterViewModel>();
                AutoMapper.Mapper.CreateMap<TabMaster, TabMasterViewModel>()
                    .ForMember(dest => dest.colID, opt => opt.MapFrom(src => src.colID));
                model.TabMasterList = AutoMapper.Mapper.Map(query.ToList(), model.TabMasterList);

                return model;
            }
            catch (System.Exception e)
            {
                this.LogError("Error getting the TabMaster list", e);
                return null;
            }
        }

if i am use following line
C#
query = query.OrderBy(x => x.colID).Skip((CurrentPage - 1) * PageSize).Take(PageSize);

then sorting is complete working as aspect.
if i am using OrderByColumn in following line, then sorting dose not work.

C#
if (OrderType.ToUpper() == "ASC")
                        query = query.OrderBy(x => OrderByColumn).Skip((CurrentPage - 1) * PageSize).Take(PageSize);
                    else
                        query = query.OrderByDescending(x => OrderByColumn).Skip((CurrentPage - 1) * PageSize).Take(PageSize);


Please suggest what i am missing?

Thanks,
Imdadhusen
Posted
Updated 10-Jun-11 3:05am
v2

This is because OrderByColumn is just a string. It is unrelated to the values that you are sorting: expression x => OrderByColumn returns the same value for all instances of x, so it makes sense that the sort does not work.

Making it work is a more interesting question: if you are good at reflection[^], you can skip the rest of this answer. If you don't like reflection, you can still do it: replace your sort expression with query.OrderBy(x => ChooseColumn(x, OrderByColumn)), and implement ChooseColumn as follows:
public object ChooseColumn(string columnName, TabMaster x) {
    switch(columnName) {
        case "MyColumn1": return x.MyColumn1;
        case "MyColumn2": return x.MyColumn2;
        case "MyColumn3": return x.MyColumn3;
        default: throw new InvalidOperationException("Unknown: "+columnName);
    }
}
 
Share this answer
 
Comments
Sunasara Imdadhusen 10-Jun-11 9:03am    
good one, but suppose i have 100+ columns then?
dasblinkenlight 10-Jun-11 9:20am    
If it's 100+ columns, your best choice is to become good at reflection: that's what the landman-code guy did :-)
Sunasara Imdadhusen 10-Jun-11 9:04am    
my vote of 5.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900