简体   繁体   中英

Remove object from array in List

I have a list of Customers, and each customer can have multiple orders, however I need to delete some of the orders from customers. If I loop through the list, I can find the Orders that I need to delete, but how do I actually delete them?

List<Customers>

public class Customers
{
    public int CustomerId { get; set; }
    public string NameFirst { get; set; }
    public Order[] Orders { get; set; }
}

public class Orders
{
    public string OrderMode { get; set; }
    public int Id { get; set; }
}

You should use List<Order> instead of an Order[] array:

public class Customers
{
    public int CustomerId { get; set; }
    public string NameFirst { get; set; }
    public List<Order> Orders { get; set; }
}

Then do the following:

customer.Orders.RemoveAll(x => x.OrderMode == "SomeOrder");

use list collection instead of array (you can't remove item in array)

        YouList.Remove (Orders);

or YouList.RemoveAt( index )

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