日期:2014-05-20  浏览次数:20941 次

帮忙看一下,排序的
C# code

     {
      if (!IsPostBack)
            {
                IsLogin();
                ViewState["SortOrder"] = "ContactNum";
                ViewState["OrderDire"] = "ascending";
                 GvDataBind();
            }
        }

  private void GvDataBind()
        {
            int year = Convert.ToInt32(Request.QueryString["ddlYear"]);
            
            var contracts = from c in db.B_Contract_Out
                            join h in db.B_Hospital on c.HospitalID equals h.ID into ch
                            where c.C4 == "1"
                            from chs in ch.DefaultIfEmpty()
                            join u in db.sys_User on c.PrincipalUser equals u.UserID into cu
                            from cus in cu.DefaultIfEmpty()
                            join p in db.B_PayRecord on c.ID equals p.ContractID into cp
                            from cps in cp.DefaultIfEmpty()
                            where c.AllMoney != Convert.ToDouble(cps.PayMoney)
                            where c.CreateTime.Value.Year == year
                            where c.ContractType == type

                            orderby c.ContractDate descending
                            orderby "c." + ViewState["SortOrder"] + " " + ViewState["OrderDire"]
                            select new
                            {
                                payMoney = GetPayedMoney(c.ID),
                                ArmNames = GetArmariumName(c.ID),
                                  ....
                                cus.U_CName,
                                qiankuan = GetQianKuan(Convert.ToDouble(c.AllMoney), c.ID)
                            };

            var cons = contracts.ToList().Where(c => GetQianKuan(Convert.ToDouble(c.AllMoney), c.ID) != 0);            
            //去除重复项
            var con = cons.Distinct();
            GvContract.DataSource = con;
            GvContract.DataBind();

     protected void GvContract_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            if (ViewState["SortOrder"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "descending")
                    ViewState["OrderDire"] = "ascending";
                else
                    ViewState["OrderDire"] = "descending";
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
            }
            GvDataBind();
        }



------解决方案--------------------
orderby "c." + ViewState["SortOrder"] + " " + ViewState["OrderDire"]

这语法是不行的

linq 不想sql那么可以拼语句
------解决方案--------------------
有些地方linq也无能为力
还是用会sql把
------解决方案--------------------
神啊!!!救救你吧
------解决方案--------------------
如果用的是Linq2EF的话,不是 "c." 是 "it."
------解决方案--------------------
这需要用到反射:

C# code
  orderby c.ContractDate descending ,GetPropertyValue(c,ViewState["SortOrder"].ToString()) ascending,GetPropertyValue(c,ViewState["OrderDire"].ToString()) ascending
    


private static object GetPropertyValue(object obj, string property)  
{  
    System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);  
    return propertyInfo.GetValue(obj, null);  
}

------解决方案--------------------