How to group list of objects c#

Viewed 130

I am calling several API's so I have a problem to group received data

For example:

  1. Api 1 is returning a list of objects and each object have different buildingId.
  2. That buildingId I need to use as an input for Api 2 which is returning list of objects (apartments) where each of the apartment have different apartmentId.
  3. Then I have to call Api 3 and use apartmentId as an input and I will receive a list of owner/s of the apartment.

Final result should be like

BuildingA
   Apartment1
      ApartmentOwner1
      ApartmentOwner2
   Apartment2
      ApartmentOwner3
   Apartment3
      ApartmentOwner4
      ApartmentOwner5
      ApartmentOwner6
BuildingB
   Apartment1
      ApartmentOwner1
      ApartmentOwner2
   Apartment2
      ApartmentOwner3
      ApartmentOwner4

I have created model for all 3 types of objects, this is example for Building.

public class BuildingResponse
    {
        public int BuildingId { get; set; }

        public string City { get; set; }

        public string Address { get; set; }
 }

I understand that I have to use foreach over data returned by Api 1 (over buildings), but how to achieve the rest of the requirement?

Thanks

1 Answers

If you have control of the API, and you know you need your building objects to be fully populated, and your apartment objects to be fully populated, you could expose an endpoint that returns fully populated buildings. That is, buildings with their apartments populated and apartments with their owners populated.

If not, one approach would be to add something like a List<ApartmentResponse> to the BuildingResponse model, and something like a List<ApartmentOwnerResponse> to the ApartmentResponse model.

Then, you iterate through the BuildingResponse models you get back from your API and request the ApartmentResponse models for that building and store it in the List<ApartmentResponse> you add to the BuildingResponse model.

Then iterate through the ApartmentResponse models, request the ApartmentOwnerResponse models from the API, and add them to the List<ApartmentOwnerResponse> you add to the ApartmentResponse model.

For example...

public class BuildingResponse
{
    // Other properties / methods
    public List<ApartmentResponse> Apartments { get; private set; } = new List<ApartmentResponse>();
}

public class ApartmentResponse
{
    // Other properties / methods
    public List<ApartmentOwnerResponse> Owners { get; private set; } = new List<ApartmentOwnerResponse>();
}

public class ApartmentOwnerResponse
{
    // Properties / methods
}

public List<BuildingResponse> GetBuildings()
{
    List<BuildingResponse> buildings = GetBuildingsFromApi();
    foreach (BuildingResponse building in buildings)
    {
        List<ApartmentResponse> apartments = GetBuildingApartmentsFromApi(building.BuildingId);
        building.Apartments.AddRange(apartments);
        foreach (ApartmentResponse apartment in apartments)
        {
            List<ApartmentOwnerResponse> owners = GetApartmentOwnersFromApi(apartment.ApartmentId);
            apartment.Owners.AddRange(owners);
        }
    }
    return buildings;
}

You should be aware that this is a N+1 problem, where you're executing one request against the API to get all the parents (BuildingResponse) then for each of those you're executing another request to get the children (ApartmentResponse). Then you're doing it again for ApartmentResponse and ApartmentOwnerResponse. This could be a performance problem, especially as your list of buildings and apartments grow.

Related