skip to Main Content

I am retrieving a list of users from Firebase but they are coming in as KVP.

{[FirstName, Eric]}
{[LastNae, EricLastName]}
{[Email, [email protected]]}
{[Phone, 12345]}

I need to convert this list of KVP to a list of Object Type named Person Class

Person
{
string FirstName {get; set;}
string LastName {get; set;}
string Email {get; set;}
string Phone {get; set;}
}

What is the most quick and efficient way of doing so:

2

Answers


  1. Not sure how the rest of your backend is architected but you can try something like using LINQ to create a new Person object for each KVP you obtained from Firebase.

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
    }
    
    public class FirebaseConverter
    {
        public List<Person> ConvertToPersonList(List<KeyValuePair<string, string>> firebaseData)
        {
            return firebaseData
                .GroupBy(kv => kv.Key)
                .Select(group => new Person
                {
                    FirstName = GetValueByKey(group, "FirstName"),
                    LastName = GetValueByKey(group, "LastName"),
                    Email = GetValueByKey(group, "Email"),
                    Phone = GetValueByKey(group, "Phone"),
                })
                .ToList();
        }
    
        private string GetValueByKey(IEnumerable<KeyValuePair<string, string>> group, string key)
        {
            return group.FirstOrDefault(kv => kv.Key == key).Value;
        }
    }
    
    

    If you don’t want to use LINQ though, you can store all the KVPs in a list and iterate through them, and then map the key-value pairs to the properties of the Person class. I would personally choose LINQ implementation though as it makes the code cleaner.

    Login or Signup to reply.
  2. Something like the one below would work as well. This includes iterating through your dictionary and assigning values to Person object.

                // Assume you have key value pair dictionary  as below
                Dictionary<string, string> firebaseDict = new Dictionary<string, string>
                {
                    { "FirstName", "Eric" },
                    { "LastName", "EricLastName" },
                    { "Email", "[email protected]" },
                    { "Phone", "12345" }
                };
        
                // You can convert the Firebase dictionary to a list of Person objects
    
                List<Person> people = new List<Person>();
                foreach (var kvp in firebaseDict)
                {
                    Person person = new Person();
                    switch (kvp.Key)
                    {
                        case "FirstName":
                            person.FirstName = kvp.Value;
                            break;
                        case "LastName":
                            person.LastName = kvp.Value;
                            break;
                        case "Email":
                            person.Email = kvp.Value;
                            break;
                        case "Phone":
                            person.Phone = kvp.Value;
                            break;
                        default:
                            // Handle unexpected keys or ignore them
                            break;
                    }
                    people.Add(person);
                }
        
                
                
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search