Kuruption
Kuruption

Reputation: 27

Adding a list item within another list

Ok I have a class similar to the following...

public class Order
{
    private Guid id;
    [DataMember]
    public Guid ID
    {
        get { return id; }
        set { id = value; }
    }

    private List<Items> orderItems;
    [DataMember]
    public List<Items> OrderItems
    {
        get { return orderItems; }
        set { orderItems= value; }
    }

}

public class Items
{
    private string itemName;
    [DataMember]
    public string ItemName
    {
        get { return itemName; }
        set { itemName = value; }
    }

}

When I reference in my code I have a method that takes in an "Order" list as the parameter.

ACME.Order newOrder = new ACME.Order();
ACME.Items newItems = new ACME.Items();

newOrder.ID = xxx
newItems.ItemName = xxx

SendOrderWithItemsFunction(newOrder)

The above works fine however I don't have an add function for my items so that I can do something like the following

newOrder.Items.Add(newItem);

and

newOrder.Items = newItems

will not work because it says that it can not implicitly convert newOrder.Items to newItems[].

What am Missing?

Upvotes: 2

Views: 10195

Answers (5)

Graham Laight
Graham Laight

Reputation: 4840

When you have a list within a list, and the Add() method is missing, a workaround is to make a new list, add the items, then set the inner list to the new list. Instead of:

outerList.innerList.Add(item)

..use..

var newList = new List<ItemType>();
newList.Add(item);
outerList.innerList = newList;

Upvotes: 0

Chris Pitman
Chris Pitman

Reputation: 13104

I think I might be missing something, but newOrder.OrderItems.Add(newItem) should work just fine, according to waht you have in your post.

Just some other nitpick things:

The pluralization of the "Items" class is wierd, if it is only a single Item. This is probably the reason that it looked "ok" to assign a single item to a List property.

You may have cut it out of your post, but every class that is being serialized by WCF must be marked as a "DataContract", not just the members of the class.

When initializing objects like this, I think it makes it a lot cleaer to use Type Initializers:

var NewOrder = new ACME.Order{
     ID = xxx,
     OrderItems = new List<ACME.Item>
     {
          new ACME.Item{
               ItemName = xxx
          }
     }
};

Upvotes: 5

jheddings
jheddings

Reputation: 27573

You have declared newItems as an ACME.Items type, but the OrderItems property of your Order class is a List<Items>. Those types are not assignable from one to the other directly. So, an assignment of newOrder.OrderItems = newItems is like trying to sayList<Items> = Items. That isn't possible based on the classes you outlined above. Instead, you will need to add to the list.

Upvotes: 0

Rashmi Pandit
Rashmi Pandit

Reputation: 23828

You should be able to do:

newOrder.OrderItems.Add(newItem);

If your newItems[] is an array, you need to do this:

newOrder.OrderItems.AddRange(newItem.ToList<Items>());

Upvotes: 0

Jon Limjap
Jon Limjap

Reputation: 95462

What you do have is an add function in your Order.OrderItems property:

newOrder.OrderItems.Add(newItem);

you can even add a whole list of items to your OrderItems:

var someList = new List<Items>();
//populate someList here
newOrder.OrderItems.AddRange(someList);

Upvotes: 0

Related Questions