Lokesh K

Lokesh K

  • NA
  • 148
  • 39.5k

multiple joins in linq query and

Oct 4 2017 1:24 PM
Hi,
 
i have joined 4 tables in a single query. in single call im getting both User Details and Next Appointment for the User. i can get the data perfectly when user has next appointment. But the scenario when there is no next appointment for user in Appointment table it returns null values even user details also.
 
can i get solution where no data in next appointment for user i should get user details.
below is my query. pls suggest me where i have done mistake.
  1. public EncounterUserDto GetUserDetailsById(Guid userId)  
  2. {  
  3. DateTime currentDate = DateTime.Now;  
  4. try  
  5. {  
  6. var userList = (_unitOfWork.Repository().GetEntityAsQuerable().Join(  
  7. _unitOfWork.Repository().GetEntityAsQuerable(), user => user.UserId, appoint => appoint.ProviderId,  
  8. (user, appoint) => new { user, appoint })  
  9. .Select(u => new  
  10. {  
  11. u.user.UserId,  
  12. u.user.FirstName,  
  13. u.user.LastName,  
  14. u.user.Title,  
  15. u.appoint.ProviderId,  
  16. u.appoint.PatientId,  
  17. u.appoint.FacilityId,  
  18. u.appoint.AppointmentId,  
  19. u.appoint.AppointmentStartDateTime  
  20. }).Where(us => us.UserId == userId).Join(  
  21. _unitOfWork.Repository().GetEntityAsQuerable(),  
  22. us => us.PatientId,  
  23. pa => pa.PatientId,  
  24. (us, pa) => new { us, pa })  
  25. .Select(pat => new  
  26. {  
  27. ProviderTitle = pat.us.Title,  
  28. ProviderFirstName = pat.us.FirstName,  
  29. ProviderLastName = pat.us.LastName,  
  30. PatientFirstName = pat.pa.FirstName,  
  31. PatientLastName = pat.pa.LastName,  
  32. pat.us.FacilityId,  
  33. pat.us.PatientId,  
  34. pat.us.AppointmentId,  
  35. pat.us.AppointmentStartDateTime,  
  36. ProviderUserId = pat.us.UserId,  
  37. pat.us.ProviderId,  
  38. }).Join(  
  39. _unitOfWork.Repository().GetEntityAsQuerable(),  
  40. ap => ap.FacilityId,  
  41. bu => bu.BusinessUnitId,  
  42. (ap, bs) => new { ap, bs })  
  43. .Select(bu => new EncounterUserDto  
  44. {  
  45. NextAppoinment = new AppointmentDto  
  46. {  
  47. AppointmentId = bu.ap.AppointmentId,  
  48. ProviderId = bu.ap.ProviderId,  
  49. PatientId = bu.ap.PatientId,  
  50. FacilityId = bu.ap.FacilityId,  
  51. StartDateTime = bu.ap.AppointmentStartDateTime.HasValue ? bu.ap.AppointmentStartDateTime : null  
  52. },  
  53. Patient = new PatientDto  
  54. {  
  55. FirstName = bu.ap.PatientFirstName,  
  56. LastName = bu.ap.PatientLastName  
  57. },  
  58. BusinessUnit = new BusinessUnitDto  
  59. {  
  60. Name = bu.bs.Name  
  61. },  
  62. FirstName = bu.ap.ProviderFirstName,  
  63. LastName = bu.ap.ProviderLastName,  
  64. Title = bu.ap.ProviderTitle  
  65. }).Where(bus => bus.NextAppoinment.StartDateTime >= currentDate)  
  66. ).FirstOrDefault();  
  67. return userList;  
  68. }  
  69. catch (Exception)  
  70. {  
  71. throw;  
  72. }  
  73. }

Answers (1)