Leonardo Wildt
Leonardo Wildt

Reputation: 2599

Add Items to Array In C#

I have a shopping cart that I iterate through in a foreach loop. Each Item in the cart needs to be added to an orderItemList array.

orderItemList = 
[{
orderItemId = 1,
quantity = 2
},{
orderItemId = 2, 
quantity =1 
}]

So I was trying to use

foreach(var items in shoppingCart)
{
var newOrderItems = new[] 
{  new orderItem { orderItemId = item.Id , quantity = item.quantity }
};

It is only adding the last item in the cart. It loops through and creates each one but only the last one is added. What am i missing?

newOrderItems only returns the one item even though it loops.

Upvotes: 0

Views: 5895

Answers (5)

user5125586
user5125586

Reputation:

Try:

for(var item in shoppingCart){
    var newOrderItems = new[] {  new orderItem { orderItemId = item.Id , quantity = item.quantity }};
    orderItemList = orderItemList.Concat(newOrderItems).ToArray();
}

Didn't get to test it but it should work.

Upvotes: 0

Rufus L
Rufus L

Reputation: 37020

Just declare your array outside the loop...here's an example using arrays:

OrderItem[] newOrderItems = new OrderItem[shoppingCart.Length];

for (int i = 0; i < shoppingCart.Length; i++)
{
    var item = shoppingCart[i];
    newOrderItems[i] = new OrderItem {orderItemId = item.Id, quantity = item.quantity};
}

There are other examples using List objects, but I think this is the coolest one-line way to convert all your shopping cart items into a List of OrderItem objects:

List<OrderItem> newOrderItems = shoppingCart.Select(item =>
    new OrderItem {orderItemId = item.Id, quantity = item.quantity}).ToList();

Upvotes: 1

IngoB
IngoB

Reputation: 2969

You need to declare var newOrderItems = new List<orderItem>(); outside your foreach loop and then use

newOrderItems.Add(new orderItem {...});

If you declare it inside, it will be created again and again.

Upvotes: 0

DavidG
DavidG

Reputation: 118937

Rather than using an array, consider using one of the generic collection classes provided by the framework instead. These are designed to be added to and removed from (among many other things. For example:

var orderItems = new List<orderItem>();

foreach(var items in shoppingCart)
{
    orderItems.Add(new orderItem 
    {
        orderItemId = item.Id, 
        quantity = item.quantity 
    });
};

You could even do this with Linq and eliminate the foreach:

var orderItems = shoppingCart
    .Select(s => new orderItem
    {
        orderItemId = item.Id, 
        quantity = item.quantity 
    })
    .ToList();

Upvotes: 1

Ray Fischer
Ray Fischer

Reputation: 996

Each time in the loop you're creating a new array (hence the "new[]") with one item in it. An array's size cannot be changed so instead use a List<> object:

newOrderItems = new List<orderItem>();
foreach(var items in shoppingCart)
{
  newOrderItems.Add(new orderItem { orderItemId = item.Id , quantity =   item.quantity});
};

A List<> can be treated like an array for almost all purposes. If you NEED an array than you can easily convert it ...

var newOrderItemsArray = newOrderItems.ToArray();

Upvotes: 2

Related Questions