代码如下:
1 public List<NetworkCollection> GetAllNetworkCollection(int pageIndex, int pageCount, string sortColumn, string descOrAsc, out int resultCount) 2 { 3 4 Expression<Func<NetworkCollection, bool>> exp = p => true; 5 6 List<NetworkCollection> list = _NHelper.QueryAll(exp).ToList(); 7 8 resultCount = _NHelper.QueryAll(exp).Count(); 9 NetworkCollection networt = new NetworkCollection(); 10 11 return _NHelper.QueryAll(exp).OrderBy(p => p.CreateDate) 12 .Skip(pageCount * pageIndex) 13 .Take(pageCount).ToList(); 14 15 }
我现在的排序列sortColumn是从前台传过来的,不是写死的createdate。有什么方法可以解决
You should use dynamic sorting via expression.
UserProfile alias = null; Expression<Func<object>> aliasExpression = () => alias; session.QueryOver<Pegfile>() .JoinAlias(x => x.UserProfiles, aliasExpression) .OrderBy(GetMemberExpression(aliasExpression, "Forename")).Asc .List(); private Expression<Func<object>> GetMemberExpression( Expression<Func<object>> aliasExpression, string property) { var propertyExpression = Expression.Property(aliasExpression.Body, typeof(UserProfile), property); var body = Expression.Convert(propertyExpression, typeof(object)); var result = Expression.Lambda<Func<object>>(body); return result; }
谢谢你的答复,用了你的方法报错了,
ERROR:could not resolve property: alias of: MyStudy.Model.Entities.NetworkCollection
public IList<NetworkCollection> GetAllNetworkCollection(int pageIndex, int pageCount, string sortColumn, string descOrAsc, out int resultCount) { try { NetworkCollection alias = null; Expression<Func<NetworkCollection>> aliasExpression = () => alias; resultCount = _NHelper.Session.QueryOver<NetworkCollection>().List().Count; return _NHelper.Session.QueryOver<NetworkCollection>() .OrderBy(GetMemberExpression(aliasExpression, sortColumn)) .Asc .List(); } catch (Exception ex) { throw ex; } } private Expression<Func<object>> GetMemberExpression( Expression<Func<NetworkCollection>> aliasExpression, string property) { var propertyExpression = Expression.Property(aliasExpression.Body, typeof(NetworkCollection), property); var body = Expression.Convert(propertyExpression, typeof(object)); var result = Expression.Lambda<Func<object>>(body); return result; }