1

I am trying to add items to Item below but I get the following error;

Cannot implicitly convert the item to System.Collection.Generic.List

My Item type is as follows;

public class Item
{
    [JsonProperty("variantId")]
    public int variantId { get; set; }

    [JsonProperty("quantity")]
    public int quantity { get; set; }

    [JsonProperty("returnReason")]
    public ReturnReason returnReason { get; set; } = new ReturnReason();
}

public class RootObject
{
    [JsonProperty("dropOff")]
    public DropOff dropOff { get; set; } = new DropOff();

    [JsonProperty("providerId")]
    public int providerId { get; set; }

    [JsonProperty("Reference")]
    public string Reference { get; set; }

    [JsonProperty("Id")]
    public int Id { get; set; }

    [JsonProperty("items")]
    public List<Item> items { get; set; } = new List<Item>();
}

I understand that they are two different types but how would I overcome this

This is throwing the error;

items = (new Item
{ 
    quantity = csv.GetField<int>(""), 
    variantId = csv.GetField<int>(""), 
    returnReason = new ReturnReason
    { 
        code= csv.GetField<int>("") 
    } 
})

Just to add more context this is how i am calling it

while (csv.Read())
{
    var booking = new ReturnsBooking.RootObject
    {
        dropOff = new DropOff {dropOffPointId = csv.GetField<string>("DropOffPointId") },
        providerId = csv.GetField<int>("ProviderID"),
        orderReference = csv.GetField("OrderReference"),
        returnMethodId = csv.GetField<int>("returnMethodId"),
        items = new Item
        {
            quantity = csv.GetField<int>(""), 
            variantId = csv.GetField<int>(""), 
            returnReason = new ReturnReason
            { 
                code = csv.GetField<int>("")
            }
        })
    };

    recordss.Add(booking);
}

return recordss;

when i try items.add i get an error stating items does not exist. I have also tried initialising Item..this doesn't seem to work either

Mayamiko
  • 105
  • 1
  • 1
  • 8
  • 1
    items.Add(new Item{....}); – Tommy Mar 13 '20 at 18:33
  • Does this answer your question? [Add multiple items to a list](https://stackoverflow.com/questions/21345005/add-multiple-items-to-a-list) – Heretic Monkey Mar 13 '20 at 18:45
  • In your `items` line you are assigning an `Item` to a `List`. Since `Item` != `List`, this should give you a compiler error. Changing it to `items = new List { new Item { ... } }` should fix it – Ivan García Topete Mar 13 '20 at 18:50

1 Answers1

4

It's throwing an error because you're not adding an item to the list, you are assigning an item to a variable that is expecting a list. This is why the exception you're getting says you can't convert item to a list.

You can use list initialization within the initialization of your RootObject:

var booking = new ReturnsBooking.RootObject
{
    items = new List<Item>
    {
        new Item
        { 
            quantity = csv.GetField<int>(""), 
            variantId = csv.GetField<int>(""), 
            returnReason = new ReturnReason
            { 
                code= csv.GetField<int>("") 
            } 
        }
    },
  //Other properties of RootObject...
}

Or you can add the item after your RootObject has been initialized like so:

var booking = new ReturnsBooking.RootObject {/* Initialize RootObject without adding the item ... */};

booking.items.Add(new Item
{ 
    quantity = csv.GetField<int>(""), 
    variantId = csv.GetField<int>(""), 
    returnReason = new ReturnReason
    { 
        code= csv.GetField<int>("") 
    } 
});
inejwstine
  • 678
  • 1
  • 10
  • 30
  • I tried this earlier but this doesn't work. Just add more context, i will add more detail to the question – Mayamiko Mar 13 '20 at 18:42
  • @Mayamiko When you say it doesn't work, what do you mean? Are you getting the same error or something different? – inejwstine Mar 13 '20 at 18:44
  • I get Invalid initializer member declarator – Mayamiko Mar 13 '20 at 18:48
  • @Mayamiko That is the correct way to add an item to a list. The "invalid initializer member declarator" is probably related to the creation of the item being added, not the operation of adding it. Checkout this question to see if it helps you with your issue: https://stackoverflow.com/q/19688230/1861453 – inejwstine Mar 13 '20 at 18:55
  • 1
    OP is using simplified object initialization, you should clarify that in order to use this syntax, he needs to access the object `items` property, not inside the initializer: `booking.items.add(new Item { ... });` – Ivan García Topete Mar 13 '20 at 18:55
  • Or, initializing the `List` with one item as in `items = new List { new Item { ... } }` – Ivan García Topete Mar 13 '20 at 18:56
  • @IvanGarcíaTopete Good catch. Added. – inejwstine Mar 13 '20 at 19:03