public ActionResult History(int carId, int userId, string sortOrder, string searchString) { var inspectionsModel = new List <InspectionViewModel>(); var inspectionList = _inspectionService.GetInspectionsForCar(carId); foreach (var inspection in inspectionList) { var model = new InspectionViewModel(inspection, userId); inspectionsModel.Add(model); } ViewBag.SortOrder = sortOrder; ViewBag.InspectionDateSortParm = String.IsNullOrEmpty(sortOrder) ? "InspectionDate_desc" : ""; ViewBag.CommentsSortParm = sortOrder == "Comments" ? "Comments_desc" : "Comments"; ViewBag.NextInspectionYearsSortParm = sortOrder == "NextInspectionYears" ? "NextInspectionYears_desc" : "NextInspectionYears"; var inspections = from inspection in inspectionsModel select inspection; if (!String.IsNullOrEmpty(searchString)) { inspections = inspections.Where(inspection => (inspection.InspectionDate != null && inspection.InspectionDate.ToString().Contains(searchString)) || (inspection.Comments != null && inspection.Comments.Contains(searchString)) || inspection.NextInspectionYears.ToString().Contains(searchString)); } switch (sortOrder) { case "InspectionDate_desc": inspections = inspections.OrderByDescending(inspection => inspection.InspectionDate); break; case "Comments": inspections = inspections.OrderBy(inspection => inspection.Comments); break; case "Comments_desc": inspections = inspections.OrderByDescending(inspection => inspection.Comments); break; case "NextInspectionYears": inspections = inspections.OrderBy(inspection => inspection.NextInspectionYears); break; case "NextInspectionYears_desc": inspections = inspections.OrderByDescending(inspection => inspection.NextInspectionYears); break; default: inspections = inspections.OrderBy(inspection => inspection.InspectionDate); break; } return(View(inspections.ToList())); }