Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TotalCount PartyId DisplayName EmailAddress PhoneNumber
- 3 1 Sarah sarah@gmail.com 1
- 3 1 Sarah sarah2@gmail.com 1
- 3 1 Sarah sarah@gmail.com 2
- DECLARE @CurrentPage int = 1
- DECLARE @PageSize int = 1000
- SELECT
- COUNT(*) OVER () as TotalCount
- , p.Id AS PartyId
- , e.EmailAddress
- , pn.PhoneNumber
- etc.....
- FROM [dbo].[Party] AS p WITH(NOLOCK)
- INNER JOIN [dbo].[Email] AS e WITH(NOLOCK) ON p.[Id] = e.[PartyID]
- INNER JOIN [dbo].[PhoneNumber] AS pn WITH(NOLOCK) ON p.[Id] = pn.[PartyID]
- etc.....
- WHERE p.PartyType = 1 /*Individual*/
- GROUP BY p.Id, e.EmailAddress, pn.PhoneNumber etc...
- ORDER BY p.Id OFFSET (@CurrentPage - 1) * @PageSize ROWS FETCH NEXT @PageSize ROWS only
- var responseModel = unitOfWork.PartyRepository.SearchContacts(model);
- if (responseModel != null && responseModel.Count == 0)
- {
- return null;
- }
- // get multiple phones/emails for a party
- var emailAddresses = responseModel.GroupBy(p => new { p.PartyId, p.EmailAddress })
- .Select(x => new {
- x.Key.PartyId,
- x.Key.EmailAddress
- });
- var phoneNumbers = responseModel.GroupBy(p => new { p.PartyId, p.PhoneNumber, p.PhoneNumberCreateDate })
- .Select(x => new {
- x.Key.PartyId,
- x.Key.PhoneNumber,
- x.Key.PhoneNumberCreateDate
- }).OrderByDescending(p => p.PhoneNumberCreateDate);
- // group by in order to avoid multiple records with different email/phones
- responseModel = responseModel.GroupBy(x => x.PartyId)
- .Select(grp => grp.First())
- .ToList();
- var list = Mapper.Map<List<SearchContactResponseModelData>>(responseModel);
- // add all phones/emails to respective party
- list = list.Select(x =>
- {
- x.EmailAddresses = new List<string>();
- x.EmailAddresses.AddRange(emailAddresses.Where(y => y.PartyId == x.PartyId).Select(y => y.EmailAddress));
- x.PhoneNumbers = new List<string>();
- x.PhoneNumbers.AddRange(phoneNumbers.Where(y => y.PartyId == x.PartyId).Select(y => y.PhoneNumber));
- return x;
- }).ToList();
- var sorted = SortAndPagination(model, model.SortBy, list);
- SearchContactResponseModel result = new SearchContactResponseModel()
- {
- Data = sorted,
- TotalCount = list.Count
- };
- return result;
- {
- "TotalCount": 1,
- "Data": [
- {
- "PartyId": 1,
- "DisplayName": "SARAH",
- "EmailAddresses": [
- "sarah@gmail.com",
- "sarah2@gmail.com"
- ],
- "PhoneNumbers": [
- "1",
- "2"
- ]
- }
- ]
- }
Add Comment
Please, Sign In to add comment