Pass List to KeyValuePair

Jassim Al Rahma 1,586 Reputation points
2022-10-24T10:03:45.913+00:00

Hi,

I want to pass the .NET MAUI Contacts and I want to send all the Contacts from device to the remote database I looping through each contact then their emails and phones will kill the performance and will be take time so I was thinking if there is a way to send the entire List of emails and List of phones at once using the KeyValuePair?

var content = new FormUrlEncodedContent(new[]  
{  
    new KeyValuePair<string, List<Phones>>("phones", all_phones),  
    new KeyValuePair<string, List<Emails>>("emails", all_emails),  
});  

Thanks,
Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,694 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,092 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,181 Reputation points Microsoft Vendor
    2022-10-26T02:59:54.203+00:00

    Hello,

    but how can I use StringContent together with my below KeyValuePair?

    If you want to use new KeyValuePair<string, string> format. You need to install Newtonsoft.Json nuget package and serialize List<Phones> and List<Emails> to JSON string like following code.

       string PhoneBodyContent= JsonConvert.SerializeObject(all_phones);  
               string EmailsBodyContent = JsonConvert.SerializeObject(all_emails);  
               
               var content = new FormUrlEncodedContent(new[]  
              {  
                new KeyValuePair<string, string>("source_uuid", "source_uuid"),  
                 new KeyValuePair<string, string>("emails", PhoneBodyContent),  
                 new KeyValuePair<string, string>("emails", EmailsBodyContent),  
            });  
    

    If sever side receive this string. you need to deserialize Json to List<Phones> and List<Emails> like following code.

       List<ContactPhone> d = JsonConvert.DeserializeObject<List<Phones>>(PhoneBodyContent);  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.