首页 新闻 会员 周边 捐助

nhibernate LINQ查询如何动态绑定排序列

0
悬赏园豆:20 [已解决问题] 解决于 2014-12-10 10:16

代码如下:

 

 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。有什么方法可以解决

A--B--C的主页 A--B--C | 初学一级 | 园豆:142
提问于:2014-12-04 11:55
< >
分享
最佳答案
0

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;
}
收获园豆:20
爱编程的大叔 | 高人七级 |园豆:30844 | 2014-12-04 12:15

谢谢你的答复,用了你的方法报错了,

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

 

 

A--B--C | 园豆:142 (初学一级) | 2014-12-04 17:33
其他回答(1)
0
A--B--C | 园豆:142 (初学一级) | 2014-12-10 10:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册