8,497
社区成员
发帖
与我相关
我的任务
分享
{
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();
}
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string orderByProperty,
bool desc) where TEntity : class
{
string command = desc ? "OrderByDescending" : "OrderBy";
var type = typeof(TEntity);
var property = type.GetProperty(orderByProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExpression = Expression.Lambda(propertyAccess, parameter);
var resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExpression));
return source.Provider.CreateQuery<TEntity>(resultExpression);
}
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
select new
{
payMoney = GetPayedMoney(c.ID),
ArmNames = GetArmariumName(c.ID),
....
cus.U_CName,
qiankuan = GetQianKuan(Convert.ToDouble(c.AllMoney), c.ID)
};
contracts = ViewState["OrderDire"] == "ascending"?contracts.OrderBy(o=>GetPropertyValue(o,ViewState["SortOrder"].ToString())) :
contracts.OrderByDescending(o=>GetPropertyValue(o,ViewState["SortOrder"].ToString()));
var cons = contracts.ToList().Where(c => GetQianKuan(Convert.ToDouble(c.AllMoney), c.ID) != 0);
//去除重复项
var con = cons.Distinct();
GvContract.DataSource = con;
GvContract.DataBind();
private static object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}