public ActionResult InspectionsHistory(string sortOrder, string searchString) { var notifiedNotifications = _inspectionService.GetHistoryInspections(); var notificationViewModel = new List <NotificationViewModel>(); foreach (var notification in notifiedNotifications) { var model = new NotificationViewModel() { NotifiedDate = notification.Notified, CarId = notification.CarId, InspectionId = notification.Id, InspectionDate = notification.InspectionDate.AddYears(notification.NextInspectionYears), Make = notification.Car.Make, Model = notification.Car.Model, Customer = notification.Car.User.LastName + " " + notification.Car.User.FirstName, ContactDetails = "P1: " + notification.Car.User.Phone1 + ",P2: " + notification.Car.User.Phone2 + ",E: " + notification.Car.User.Email, Comment = notification.Comments, RegistrationNumber = notification.Car.RegistrationNumber }; notificationViewModel.Add(model); } ViewBag.SortOrder = sortOrder; ViewBag.NotifiedDateSortParm = String.IsNullOrEmpty(sortOrder) ? "NotifiedDate_desc" : ""; ViewBag.InspectionDateSortParm = sortOrder == "InspectionDate" ? "InspectionDate_desc" : "InspectionDate"; ViewBag.MakeSortParm = sortOrder == "Make" ? "Make_desc" : "Make"; ViewBag.ModelSortParm = sortOrder == "Model" ? "Model_desc" : "Model"; ViewBag.CustomerSortParm = sortOrder == "Customer" ? "Customer_desc" : "Customer"; ViewBag.ContactDetailsSortParm = sortOrder == "ContactDetails" ? "ContactDetails_desc" : "ContactDetails"; ViewBag.CommentSortParm = sortOrder == "Comment" ? "Comment_desc" : "Comment"; ViewBag.RegistrationNumberSortParm = sortOrder == "RegistrationNumber" ? "RegistrationNumber_desc" : "RegistrationNumber"; var Inspections = from Inspection in notificationViewModel select Inspection; if (!String.IsNullOrEmpty(searchString)) { Inspections = Inspections.Where(Inspection => Inspection.NotifiedDate.ToString().Contains(searchString) || (Inspection.InspectionDate != null && Inspection.InspectionDate.ToString().Contains(searchString)) || Inspection.Make.Contains(searchString) || Inspection.Model.Contains(searchString) || Inspection.Customer.Contains(searchString) || Inspection.ContactDetails.Contains(searchString) || (Inspection.Comment != null && Inspection.Comment.Contains(searchString)) || Inspection.RegistrationNumber.Contains(searchString)); } switch (sortOrder) { case "NotifiedDate_desc": Inspections = Inspections.OrderByDescending(Inspection => Inspection.NotifiedDate); break; case "InspectionDate": Inspections = Inspections.OrderBy(Inspection => Inspection.InspectionDate); break; case "InspectionDate_desc": Inspections = Inspections.OrderByDescending(Inspection => Inspection.InspectionDate); break; case "Make": Inspections = Inspections.OrderBy(Inspection => Inspection.Make); break; case "Make_desc": Inspections = Inspections.OrderByDescending(Inspection => Inspection.Make); break; case "Model": Inspections = Inspections.OrderBy(Inspection => Inspection.Model); break; case "Model_desc": Inspections = Inspections.OrderByDescending(Inspection => Inspection.Model); break; case "Customer": Inspections = Inspections.OrderBy(Inspection => Inspection.Customer); break; case "Customer_desc": Inspections = Inspections.OrderByDescending(Inspection => Inspection.Customer); break; case "ContactDetails": Inspections = Inspections.OrderBy(Inspection => Inspection.ContactDetails); break; case "ContactDetails_desc": Inspections = Inspections.OrderByDescending(Inspection => Inspection.ContactDetails); break; case "Comment": Inspections = Inspections.OrderBy(Inspection => Inspection.Comment); break; case "Comment_desc": Inspections = Inspections.OrderByDescending(Inspection => Inspection.Comment); break; case "RegistrationNumber": Inspections = Inspections.OrderBy(Inspection => Inspection.RegistrationNumber); break; case "RegistrationNumber_desc": Inspections = Inspections.OrderByDescending(Inspection => Inspection.RegistrationNumber); break; default: Inspections = Inspections.OrderBy(Inspection => Inspection.NotifiedDate); break; } return(View(Inspections.ToList())); }