可能重复:
动态 LINQ OrderBy
我正在尝试为 Iqueryable 创建一个动态排序。
所以下面你可以看到我正在关注我在stackoverflow中看到的一些示例。
var query = dalSession.Query<T>();
var res = (from x in query orderby Extensions.Sort<T>(query, "FirstName") select x).Skip((paging.CurrentPageRecord)).Take(paging.PageSize);
public static class Extensions
{
public static IQueryable<T> Sort<T>(this IQueryable<T> query,
string sortField)
{
return query.OrderByDescending(s => s.GetType()
.GetProperty(sortField));
}
}
这是我得到的例外:
System.Linq.IQueryable
1[Partners.BusinessObjects.Affiliate] Sort[Affiliate](System.Linq.IQueryable
1[Partners.BusinessObjects.Affiliate],System.String)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: System.Linq.IQueryable`1[Partners.BusinessObjects.Affiliate] Sort[Affiliate](System.Linq.IQueryable`1[Partners.BusinessObjects.Affiliate], System.String)