code_junkie

How do I serialize an anonymous object for sending over a SOAP web service?

Nov 12th, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. // Some code has been removed here to simplify the example.
  2. [WebMethod(EnableSession = true)]
  3. public Response GetPatientList() {
  4. var patientList = from patient in ((User)Session["user"]).Practice.Patients
  5. select new {
  6. patientID = patient.PatientID,
  7. status = patient.Active ? "Active" : "Inactive",
  8. patientIdentifier = patient.PatientIdentifier,
  9. physician = (patient.Physician.FirstName + " " + patient.Physician.LastName).Trim(),
  10. lastModified = patient.Visits.Max(v => v.VisitDate)
  11. };
  12. return patientList;
  13. }
  14.  
  15. public class Result {
  16. public bool success;
  17. public bool loggedIn;
  18. public string message;
  19. }
  20.  
  21. public class PracticeInfoResult : Result {
  22. public string practiceName;
  23. public string address;
  24. public string city;
  25. public string state;
  26. public string zipCode;
  27. public string phone;
  28. public string fax;
  29. }
  30.  
  31. [WebMethod(EnableSession = true)]
  32. public PatientsResult GetPatientList(bool returnInactivePatients) {
  33. if (!IsLoggedIn()) {
  34. return new PatientsResult() {
  35. Success = false,
  36. LoggedIn = false,
  37. Message = "Not logged in"
  38. };
  39. }
  40. Func<IEnumerable<PatientResult>, IEnumerable<PatientResult>> filterActive =
  41. patientList => returnInactivePatients ? patientList : patientList.Where(p => p.Status == "Active");
  42. User u = (User)Session["user"];
  43. return new PatientsResult() {
  44. Success = true,
  45. LoggedIn = true,
  46. Message = "",
  47. Patients = filterActive((from p in u.Practice.Patients
  48. select new PatientResult() {
  49. PhysicianID = p.PhysicianID,
  50. Status = p.Active ? "Active" : "Inactive",
  51. PatientIdentifier = p.PatientIdentifier,
  52. PatientID = p.PatientID,
  53. LastVisit = p.Visits.Count > 0 ? p.Visits.Max(v => v.VisitDate).ToShortDateString() : "",
  54. Physician = (p.Physician == null ? "" : p.Physician.FirstName + " " + p.Physician == null ? "" : p.Physician.LastName).Trim(),
  55. })).ToList<PatientResult>()
  56. };
  57. }
  58. public class Result {
  59. public bool Success { get; set; }
  60. public bool LoggedIn { get; set; }
  61. public string Message { get; set; }
  62. }
  63. public class PatientsResult : Result {
  64. public List<PatientResult> Patients { get; set; }
  65. }
  66. public class PatientResult {
  67. public int PatientID { get; set; }
  68. public string Status { get; set; }
  69. public string PatientIdentifier { get; set; }
  70. public string Physician { get; set; }
  71. public int? PhysicianID {get;set;}
  72. public string LastVisit { get; set; }
  73. }
Add Comment
Please, Sign In to add comment