Search code examples
c#ienumerable

Add and populate an IEnumerable property to an API call


I'm using an older 3rd party API to connect to a legacy system.

Here is the code:

    AutoMaximizer.AuthUser yourCredentials = new AutoMaximizer.AuthUser
    {
        UserKey = "x1213"
    };

    AutoMaximizer.Pagers availableCars = new AutoMaximizer.Pagers
    {
        TotalCalls = 65,
        Router = 91220
    };

    ISessionManagement s = new ManagementClient();

    FactoryResponse response;

    response = await s.GetAll(yourCredentials, new ListRequest
    {
        Pagers = availableCars,
        SortIncreasing = true
    });

It's working, but I want to add another property when I make the request.

The property is called Types and is a IEnumerable<Type>. The API docs state:

Types   =  Gets or sets an enumeration of types to include in the response.

And in the API reference, I found it:

public enum Type : int
{
    
    [System.Runtime.Serialization.EnumMemberAttribute()]
    Auto = 0,
    
    [System.Runtime.Serialization.EnumMemberAttribute()]
    Truck = 1,
    
    [System.Runtime.Serialization.EnumMemberAttribute()]
    Motorcycle = 2
    
}

But I'm not quite sure how to add it to the GetAll method.

I tried adding this:

List<AutoMaximizer.Type> types = new List<AutoMaximizer.Type>();
types.Add(AutoMaximizer.Type.Auto);
types.Add(AutoMaximizer.Type.Truck);
types.Add(AutoMaximizer.Type.Motorcycle);

And then this:

response = await s.GetAll(yourCredentials, new ListRequest
{
    Pagers = availableCars,
    SortIncreasing = true,
    Types = types
});

But that gives me this error:

Cannot implicitly convert type Systems.Collections.Generic.List<AutoMaximizer.Type> to AutoMaximizer.Type[]

I am not sure what to do now...

Is there a way to get this to work?

Thanks!


Solution

  • Read your error, it wants an array, not a list:

    response = await s.GetAll(yourCredentials, new ListRequest
    {
        Pagers = availableCars,
        SortIncreasing = true,
        Types = new[] { Type.Auto, Type.Truck, Type.Motorcycle },
    });