imdadhusen
imdadhusen

Reputation: 2494

Sorting does not work with dynamic column

I have one generic function like:

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

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.

 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,

Upvotes: 0

Views: 120

Answers (3)

imdadhusen
imdadhusen

Reputation: 2494

Here is the Excellent solution for the same

http://landman-code.blogspot.com/2008/11/linq-to-entities-string-based-dynamic.html

Thanks a lot Davy Landman for excellent solution.

Upvotes: 1

Amir Ismail
Amir Ismail

Reputation: 3883

Try to use reflection to get OrderByColumn dynamically:

query = query.OrderBy(x => typeof(x).GetProperty(OrderByColumn)).Skip((CurrentPage - 1) * PageSize).Take(PageSize);

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039110

You may try dynamic LINQ. It's more adapted to the kind of dynamic queries that you are trying to implement.

Upvotes: 0

Related Questions