public ActionResult AjaxHandler(tblUserParamModel param) { var allResult = db.getUsers(string.IsNullOrEmpty(param.Donvi) ? "" : param.Donvi).ToList(); IEnumerable <getUsers_Result> filteredResult; //Check whether the companies should be filtered by keyword if (!string.IsNullOrEmpty(param.sSearch)) { //Optionally check whether the columns are searchable at all var Searchable_0 = Convert.ToBoolean(Request["bSearchable_0"]); var Searchable_2 = Convert.ToBoolean(Request["bSearchable_2"]); var Searchable_3 = Convert.ToBoolean(Request["bSearchable_3"]); var Searchable_4 = Convert.ToBoolean(Request["bSearchable_4"]); var Searchable_6 = Convert.ToBoolean(Request["bSearchable_6"]); int tmp = int.TryParse(param.sSearch, out tmp) ? tmp : 0; filteredResult = allResult .Where(c => Searchable_2 && c.HoTen.ToLower().Contains(param.sSearch.ToLower()) || Searchable_3 && c.TenDangNhap.ToLower().Contains(param.sSearch.ToLower()) || Searchable_4 && c.Email.ToLower().Contains(param.sSearch.ToLower()) || Searchable_6 && c.DonVi.ToLower().Contains(param.sSearch.ToLower()) || Searchable_0 && c.STT.Equals(tmp) ); } else { filteredResult = allResult; } var Sortable_0 = Convert.ToBoolean(Request["bSortable_0"]); var Sortable_2 = Convert.ToBoolean(Request["bSortable_2"]); var Sortable_3 = Convert.ToBoolean(Request["bSortable_3"]); var Sortable_4 = Convert.ToBoolean(Request["bSortable_4"]); var Sortable_6 = Convert.ToBoolean(Request["bSortable_6"]); var sortColumnIndex = Convert.ToInt64(Request["iSortCol_0"]); Func <getUsers_Result, string> orderingFunction = (c => sortColumnIndex == 2 && Sortable_2 ? c.HoTen : sortColumnIndex == 3 && Sortable_3 ? c.TenDangNhap : sortColumnIndex == 4 && Sortable_4 ? c.Email : sortColumnIndex == 6 && Sortable_6 ? c.DonVi : ""); Func <getUsers_Result, Int64> orderingFunction2 = (c => sortColumnIndex == 0 && Sortable_0 ? c.STT : 0); var sortDirection = Request["sSortDir_0"]; // asc or desc if (sortDirection == "asc") { filteredResult = filteredResult.OrderBy(orderingFunction).ThenBy(orderingFunction2); } else { filteredResult = filteredResult.OrderByDescending(orderingFunction).ThenByDescending(orderingFunction2); } var displayed = filteredResult.Skip(param.iDisplayStart).Take(param.iDisplayLength); var result = displayed.Select(c => new { col0 = c.STT, col1 = c.ID, col2 = c.HoTen, col3 = c.TenDangNhap, col4 = c.Email, col5 = ListToString(UserManager.GetRoles(c.ID).ToList()), col6 = c.DonVi }); return(Json(new { sEcho = param.sEcho, iTotalRecords = allResult.Count(), iTotalDisplayRecords = filteredResult.Count(), aaData = result }, JsonRequestBehavior.AllowGet)); }