1

I have a class:

public class FlightDetails
{
    public string FlightId {get; set;}
    public string PilotName {get; set;}
    public string Area {get; set;}
    public string Country {get; set;}
}

Here sending response:

public async Task<List<FlightDetails>> GetFlightAsync(string FlightId)
{
    //
    var flights = new List<FlightDetails>();
    flights = response.AllFlights;
    flights = flights.Where(x => x.FlightId.Contains(FlightId)).ToList();
    //
    return flights;
}

Getting List here and data is filled but issue is don't want FlightId and Country in the response which I am sending. How to remove this objects in the List? Finally in the List item there should be only PilotName and Area.


Update:

I forgot the following line before the flights = response.AllFlights;

    var request = await _rest.Get<WorldFlights>(url + $"?FlightId={FlightId}");
alex dave
  • 115
  • 1
  • 4
  • 10
  • @alexdave - Please don't edit your question so as to make existing answers look incomplete. Adding the `var request = ...` line does that and it makes your code look faulty. If you're adding new information do it at the bottom of your question and clearly mark it as an update to the original question. – Enigmativity Aug 07 '22 at 21:53
  • @Enigmativity sorry, I missed await line in this. So added. Also Pau which he wrote creates an error because I felt this code makes understand less. – alex dave Aug 07 '22 at 21:56
  • @alexdave - I fixed it for you. – Enigmativity Aug 07 '22 at 22:01
  • Your code now has a gap between the `request` and the `response`. – Enigmativity Aug 07 '22 at 22:02

1 Answers1

0

You will need to create another object, and map there only the properties you want. For example:

public class FlightDetailsResponse
{
    public string PilotName {get; set;}
    public string Area {get; set;}
}

And then in the function:

public async Task<List<FlightDetailsResponse>> GetFlightAsync(string FlightId){
    
    var flights = response.AllFlights;
    var flightResponses = flights
        .Where(x => x.FlightId.Contains(FlightId).ToList())
        .Select(x => new FlightDetailsResponse{
            PilotName = x.PilotName,
            Area = x.Area
        });
    
    return flightResponses;
}

This way the response will only contain the PilotName and Area fields.

PD: What I wrote might not compile because of missing commas or something like that. Sorry, it has been some time since I last wrote C#, but the idea is that.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Pau
  • 138
  • 6
  • I already tried this kind of approach but used to get implicit List to List conversion error. – alex dave Aug 07 '22 at 21:30
  • You could try with ".ToList()" at the end of the Select. But please provide the exact error that you are getting. – Pau Aug 07 '22 at 21:38
  • toList() is a typo, I corrected it. this approach gives cannot implicitly convert type error. – alex dave Aug 08 '22 at 05:04
  • These things are typically called "DTOs", so I would rename the class `FlightDetailsDto` in your snippets. Generally speaking, it's good practice not to expose your domain objects, but only DTOs. https://stackoverflow.com/questions/1051182/what-is-a-data-transfer-object-dto – Casper Bang Aug 08 '22 at 11:01