简体   繁体   中英

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.

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();

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();

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();

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM