[已解决]:排序不适用于动态列 [英] [SOLVED]: Sorting does not work with dynamic column

查看:124
本文介绍了[已解决]:排序不适用于动态列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用函数,例如:

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);


然后排序就可以完成.
如果我在下一行中使用OrderByColumn,则排序不起作用.


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);



请提出我想念的东西吗?

谢谢,
Imdadhusen



Please suggest what i am missing?

Thanks,
Imdadhusen

推荐答案

这是因为OrderByColumn只是一个字符串.它与您要排序的值无关:表达式x => OrderByColumn对于x的所有实例返回相同的值,因此有意义的是排序不起作用.

使它起作用是一个更有趣的问题:如果您擅长 reflection [ ^ ],您可以跳过此答案的其余部分.如果您不喜欢反射,您仍然可以这样做:用query.OrderBy(x => ChooseColumn(x, OrderByColumn))替换您的排序表达式,并按如下方式实现ChooseColumn:
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);
    }
}


相同的解决方案

http://landman-code.blogspot.com/2008/11/linq-to-entities-string-based-dynamic.html [
Here is the Excellent solution for the same

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


这篇关于[已解决]:排序不适用于动态列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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