public IActionResult Patients(string sortOrder) { ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; var patientsObs = _dataSourceService.GetListData <ListObservation>("Observation", 0).ToList(); var patientsObsEnu = (IEnumerable <ListObservation>)patientsObs; patientsObsEnu = patientsObsEnu.Where(x => x.subject != null); patientsObsEnu = patientsObsEnu.Where(x => x.subject.reference != null); patientsObsEnu = patientsObsEnu.Where(x => x.subject.reference.Split('/').Count() == 2); var ids = patientsObsEnu.Select(x => x.subject.reference.Split('/')[1]).ToList(); ids.AddRange(TempValid); var uniqIds = ids.ToHashSet(); var patients = new List <ListPatient>(); foreach (var id in uniqIds) { patients.Add(_dataSourceService.GetData <ListPatient>($"Patient/{id}")); Console.Write(@$ "" "{id}" ","); } patients = patients.Where(x => x.name != null).ToList(); patients = patients.Where(x => x.name.Count >= 1).ToList(); patients = patients.Where(x => x.name.First().given != null).ToList(); if (sortOrder == "name_desc") { patients = patients.OrderByDescending(x => x.name.First().family).ToList(); } else { patients = patients.OrderBy(x => x.name.First().family).ToList(); } return(View(patients)); }