[已解决]:排序不适用于动态列
问题描述:
我有一个通用函数,例如:
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 becauseOrderByColumn
is just a string. It is unrelated to the values that you are sorting: expressionx => OrderByColumn
returns the same value for all instances ofx
, 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 withquery.OrderBy(x => ChooseColumn(x, OrderByColumn))
, and implementChooseColumn
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[^]