0

I been working on this API that I would like to remove the top element but I am not sure the best way to do it. Do I need to

 public CARlinesResponse GetCarslinesResponse (string accessKey)
            CARlinesResponse CAR = new CARlinesResponse();
            CarlineManager cm = new CarlineManager ();
            CarslineReportParameters cr= new CarslineReportParameters ();
            car.lines = GetGuidelineViewModel(cm.GetCarslinesResponse (cr));

           return CAR;
           //return CAR.CARline;
        }

Model

namespace WebApi.Models
{
    public class CARlinesResponse
    {
        public CARline[] CARlines{ get; set; }
    }

Output I am looking for

[
    {
        "Id": "9c996a94-5a61-42b4-953e-34134f6332b3",
        "Title": "Jeep",
        "Version": "grand cherokee",

    }
]

Like to remove the top level cars

 {
        "CARline": [
            {
                "Id": "9c996a94-5a61-42b4-953e-34134f6332b3",
                "Title": "Jeep",
                "Version": "grand cherokee"
            }
3
  • yeah you should just be able to return Car.Carline; - you'd also have to modify your method signature accordingly. Is there a problem when you do that? Commented Nov 13, 2018 at 15:31
  • Error cannot convert type Models.CarLine[] to CarLinesResponse. Commented Nov 13, 2018 at 15:33
  • as I mentioned - you'd have to modify your method signature accordingly. See answer below. Of course any code which calls the method might also need to be changed, it depends what you've implemented. Commented Nov 13, 2018 at 15:34

1 Answer 1

1

I would expect that returning the Carline property, with an appropriately amended method signature, should do the trick.

public CARline[] GetCarslinesResponse (string accessKey)
{
  CARlinesResponse Car = new CARlinesResponse();
  CarlineManager cm = new CarlineManager ();
  CarslineReportParameters cr= new CarslineReportParameters ();
  Car.Carlines = GetGuidelineViewModel(cm.GetCarslinesResponse (cr));

  return Car.CARlines;
}

Of course any code which calls the method might also need to be changed, it depends what exactly you've implemented.

P.S. It seems you could actually shorten this to

public CARline[] GetCarslinesResponse (string accessKey)
{
  CarlineManager cm = new CarlineManager ();
  CarslineReportParameters cr = new CarslineReportParameters ();
  return GetGuidelineViewModel(cm.GetCarslinesResponse (cr));
}

and dispense with the CarLinesRepsonse object entirely.

Sign up to request clarification or add additional context in comments.

8 Comments

when I change it to CAR.Carline I get implicitly convert type WS.Car.Model.Carline[] to WEBAPI.Model.CarLineReponse.
As I mentioned in the answer, any code which calls this method might also need to be changed. You haven't shown that calling code so I can't tell you what exactly to change. But you need to understand that C# is a strongly-typed language - if you're going to call a method, you have to make sure you read the result into a variable of the matching type.
P.S. Please don't edit your question like that, with no explanation. it makes the comments and answers seem nonsensical. If you have an update, please add it as a separate section of code underneath the original. Thanks.
I have added the model
ok but you haven't added the code which calls the GetCarLinesResponse method. That's where you need to make a change.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.