Hi,
Please refer below code:
namespace MSTestProject
{
[TestClass]
public class TestLinkFlightClass
{
[TestMethod]
public void TestLinkFlight()
{
var airportResponse = GetAirportResponse();
var airports = Enumerable.Empty<Airport>().ToList();
if (airportResponse != null && airportResponse.Airports != null && airportResponse.Airports.Count > 0)
{
airports = airportResponse.Airports.DistinctBy(x => x.iata_code).ToList();
}
var flightScheduleResposne = GetFlightScheduleResponse();
var flightSchedules = Enumerable.Empty<FlightSchedule>().ToList();
if (flightScheduleResposne != null && flightScheduleResposne.FlightSchedules != null && flightScheduleResposne.FlightSchedules.Count > 0)
{
var flightScheduleWithAirports = SetFlightScheduleAirport(flightScheduleResposne.FlightSchedules, airports);
foreach (var item in flightScheduleWithAirports)
{
Console.WriteLine($"The name of dep airport of {item.dep_iata}:");
Console.WriteLine(item.DepAirport.name);
Console.WriteLine(item.DepAirport.names.ar);//name for mutiple language
Console.WriteLine($"The name of arr airport of {item.arr_iata}:");
Console.WriteLine(item.ArrAirport.name);
Console.WriteLine(item.ArrAirport.names.cs);//name for mutiple language
}
}
}
IList<FlightScheduleWithAirport> SetFlightScheduleAirport(IList<FlightSchedule> flightSchedules, IList<Airport> airports)
{
var flightScheduleWithAirport = flightSchedules.Select(x => x.ToFlightScheduleWithName()).ToList();
foreach (var item in flightScheduleWithAirport)
{
item.DepAirport = airports.FirstOrDefault(x => item.dep_iata == x.iata_code);
item.ArrAirport = airports.FirstOrDefault(x => item.arr_iata == x.iata_code);
}
return flightScheduleWithAirport;
}
AirportResponse GetAirportResponse()
{
//You logic for get the AirportResponse from API
//Below code is only for test
return new AirportResponse()
{
Airports = new List<Airport>
{
new Airport
{
name = "DepAirport1",
iata_code ="DepCode1",
names = new Names{ ar = "DepAirport1_ar", cs = "DepAirport1_cs" }//...othe names property
},
new Airport
{
name = "DepAirport2",
iata_code ="DepCode2",
names = new Names{ ar = "DepAirport2_ar", cs = "DepAirport2_cs" }//...othe names property
},
new Airport
{
name = "ArrAirport1",
iata_code ="ArrCode1",
names = new Names{ ar = "ArrAirport1_ar", cs = "ArrAirport1_cs" }//...othe names property
},
new Airport
{
name = "ArrAirport2",
iata_code ="ArrCode2",
names = new Names{ ar = "ArrAirport2_ar", cs = "ArrAirport2_cs" }//...othe names property
}
}
};
}
FlightScheduleResponse GetFlightScheduleResponse()
{
//You logic for get the FlightScheduleResponse from API
//Below code is only for test
return new FlightScheduleResponse
{
FlightSchedules = new List<FlightSchedule>
{
new FlightSchedule{ dep_iata ="DepCode1",arr_iata="ArrCode1" },
new FlightSchedule{ dep_iata ="DepCode2",arr_iata="ArrCode2" }
}
};
}
}
public class FlightScheduleResponse
{
public IList<FlightSchedule> FlightSchedules { get; set; }
}
public class FlightScheduleWithAirport : FlightSchedule
{
public Airport DepAirport { get; set; }
public Airport ArrAirport { get; set; }
}
public class FlightSchedule
{
public string airline_iata { get; set; }
public string airline_icao { get; set; }
public string flight_iata { get; set; }
public string flight_icao { get; set; }
public string flight_number { get; set; }
public string cs_airline_iata { get; set; }
public string cs_flight_number { get; set; }
public string cs_flight_iata { get; set; }
public string dep_iata { get; set; }
public string dep_icao { get; set; }
public string dep_terminal { get; set; }
public string dep_gate { get; set; }
public string dep_time { get; set; }
public int dep_time_ts { get; set; }
public string dep_time_utc { get; set; }
public string dep_estimated { get; set; }
public int dep_estimated_ts { get; set; }
public string dep_estimated_utc { get; set; }
public string arr_iata { get; set; }
public string arr_icao { get; set; }
public string arr_terminal { get; set; }
public string arr_gate { get; set; }
public string arr_baggage { get; set; }
public string arr_time { get; set; }
public int arr_time_ts { get; set; }
public string arr_time_utc { get; set; }
public string arr_estimated { get; set; }
public int arr_estimated_ts { get; set; }
public string arr_estimated_utc { get; set; }
public string status { get; set; }
public int duration { get; set; }
public int delayed { get; set; }
public FlightScheduleWithAirport ToFlightScheduleWithName()
{
return new FlightScheduleWithAirport
{
dep_iata = dep_iata,
arr_iata = arr_iata,
///....Other property to set
///...
///...
///...
///...
///...
DepAirport = default,
ArrAirport = default
};
}
}
public class AirportResponse
{
public IList<Airport> Airports { get; set; }
}
public class Airport
{
public string name { get; set; }
public string iata_code { get; set; }
public string icao_code { get; set; }
public float lat { get; set; }
public float lng { get; set; }
public int alt { get; set; }
public string city { get; set; }
public string city_code { get; set; }
public string un_locode { get; set; }
public string timezone { get; set; }
public string country_code { get; set; }
public string phone { get; set; }
public string website { get; set; }
public string facebook { get; set; }
public string instagram { get; set; }
public string linkedin { get; set; }
public string twitter { get; set; }
public Names names { get; set; }
public int runways { get; set; }
public int departures { get; set; }
public int connections { get; set; }
public bool is_major { get; set; }
public int is_international { get; set; }
public string slug { get; set; }
}
public class Names
{
public string no { get; set; }
public string de { get; set; }
public string hi { get; set; }
public string ln { get; set; }
public string ru { get; set; }
public string fi { get; set; }
public string pt { get; set; }
public string jv { get; set; }
public string fr { get; set; }
public string hu { get; set; }
public string wuu { get; set; }
public string uk { get; set; }
public string sk { get; set; }
public string id { get; set; }
public string mk { get; set; }
public string sv { get; set; }
public string ko { get; set; }
public string pnb { get; set; }
public string mr { get; set; }
public string el { get; set; }
public string en { get; set; }
public string _is { get; set; }
public string it { get; set; }
public string ta { get; set; }
public string es { get; set; }
public string cs { get; set; }
public string ar { get; set; }
public string vi { get; set; }
public string th { get; set; }
public string ja { get; set; }
public string fa { get; set; }
public string pl { get; set; }
public string ro { get; set; }
public string he { get; set; }
public string tr { get; set; }
public string nl { get; set; }
}
}
If right, please Accept.
Enjoy programming!!!