protected void gvSearchResult_Sorting(object sender, GridViewSortEventArgs e) { Expression <Func <Claim, bool> > predicate = null; predicate = buildPredicate(); IQueryable <LeadView> leads = LeadsManager.SearchClaim(predicate); bool descending = false; if (ViewState[e.SortExpression] == null) { descending = false; } else { descending = !(bool)ViewState[e.SortExpression]; } ViewState[e.SortExpression] = descending; gvSearchResult.DataSource = leads.orderByExtension(e.SortExpression, descending); gvSearchResult.DataBind(); }