Guest User

Untitled

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