根据一个变量的值
确定orderby的条件我现在是根据变量的值改整个linq to sql语句,现在想把orderby提取出来

解决方案 »

  1.   


    private static object GetPropertyValue(object obj, string property)  
    {  
        System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);  
        return propertyInfo.GetValue(obj, null);  
    }  
    http://blog.csdn.net/q107770540/archive/2011/01/13/6133484.aspx
      

  2.   

    http://blog.csdn.net/Sandy945/archive/2010/07/14/5735326.aspx动态构建OrderBy的Lambda表达式
      

  3.   

    可以通过orderby语句或集合.OrderBy使用
      

  4.   

    下面列子看一下就知道了:     
           NewsManagerDataContext dc = new NewsManagerDataContext();
                var source = dc.NewsInfo
                    .Skip(PageIndex * GvNewsList.PageSize)
                    .Take(GvNewsList.PageSize)
                    .Select(b => new
                    {
                        Id = b.Aid,
                        标题 = b.Title,
                        内容 = b.Content,
                        创建人 = b.Person,
                        创建时间 = b.DateTime,
                        点击率 = b.clickRate
                    }
                    )
                    .OrderBy(b => b.标题)
                    .OrderBy(b => b.创建人)
                    //.OrderBy(b => b.创建时间)
                    .OrderBy(b => b.点击率)
                    .OrderBy(b => b.内容)
                    .OrderByDescending(b => b.创建时间);            this.GvNewsList.DataSource = source;
                this.GvNewsList.DataBind();
                this.lblTotalPage.Text = GetTotalCount().ToString(); //总页数
                InitDdl();  //绑定当前页
      

  5.   

    先谢谢楼上的几位
    加菲猫的GetPropertyValue方法能不能再加入一些表达式,比如对一列的值修改后再排序,升序、降序等等