Guest User

Untitled

a guest
Mar 9th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. TotalCount PartyId DisplayName EmailAddress PhoneNumber
  2. 3 1 Sarah sarah@gmail.com 1
  3. 3 1 Sarah sarah2@gmail.com 1
  4. 3 1 Sarah sarah@gmail.com 2
  5.  
  6. DECLARE @CurrentPage int = 1
  7. DECLARE @PageSize int = 1000
  8.  
  9. SELECT
  10. COUNT(*) OVER () as TotalCount
  11. , p.Id AS PartyId
  12. , e.EmailAddress
  13. , pn.PhoneNumber
  14. etc.....
  15.  
  16. FROM [dbo].[Party] AS p WITH(NOLOCK)
  17. INNER JOIN [dbo].[Email] AS e WITH(NOLOCK) ON p.[Id] = e.[PartyID]
  18. INNER JOIN [dbo].[PhoneNumber] AS pn WITH(NOLOCK) ON p.[Id] = pn.[PartyID]
  19. etc.....
  20.  
  21.  
  22. WHERE p.PartyType = 1 /*Individual*/
  23. GROUP BY p.Id, e.EmailAddress, pn.PhoneNumber etc...
  24. ORDER BY p.Id OFFSET (@CurrentPage - 1) * @PageSize ROWS FETCH NEXT @PageSize ROWS only
  25.  
  26. var responseModel = unitOfWork.PartyRepository.SearchContacts(model);
  27. if (responseModel != null && responseModel.Count == 0)
  28. {
  29. return null;
  30. }
  31. // get multiple phones/emails for a party
  32. var emailAddresses = responseModel.GroupBy(p => new { p.PartyId, p.EmailAddress })
  33. .Select(x => new {
  34. x.Key.PartyId,
  35. x.Key.EmailAddress
  36. });
  37.  
  38. var phoneNumbers = responseModel.GroupBy(p => new { p.PartyId, p.PhoneNumber, p.PhoneNumberCreateDate })
  39. .Select(x => new {
  40. x.Key.PartyId,
  41. x.Key.PhoneNumber,
  42. x.Key.PhoneNumberCreateDate
  43. }).OrderByDescending(p => p.PhoneNumberCreateDate);
  44.  
  45.  
  46.  
  47.  
  48. // group by in order to avoid multiple records with different email/phones
  49. responseModel = responseModel.GroupBy(x => x.PartyId)
  50. .Select(grp => grp.First())
  51. .ToList();
  52.  
  53. var list = Mapper.Map<List<SearchContactResponseModelData>>(responseModel);
  54.  
  55. // add all phones/emails to respective party
  56. list = list.Select(x =>
  57. {
  58. x.EmailAddresses = new List<string>();
  59. x.EmailAddresses.AddRange(emailAddresses.Where(y => y.PartyId == x.PartyId).Select(y => y.EmailAddress));
  60.  
  61. x.PhoneNumbers = new List<string>();
  62. x.PhoneNumbers.AddRange(phoneNumbers.Where(y => y.PartyId == x.PartyId).Select(y => y.PhoneNumber));
  63. return x;
  64. }).ToList();
  65.  
  66. var sorted = SortAndPagination(model, model.SortBy, list);
  67.  
  68. SearchContactResponseModel result = new SearchContactResponseModel()
  69. {
  70. Data = sorted,
  71. TotalCount = list.Count
  72. };
  73.  
  74. return result;
  75.  
  76. {
  77. "TotalCount": 1,
  78. "Data": [
  79. {
  80. "PartyId": 1,
  81. "DisplayName": "SARAH",
  82. "EmailAddresses": [
  83. "sarah@gmail.com",
  84. "sarah2@gmail.com"
  85. ],
  86. "PhoneNumbers": [
  87. "1",
  88. "2"
  89. ]
  90. }
  91. ]
  92. }
Add Comment
Please, Sign In to add comment