2

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; }
}
1
  • 1
    Just a semantic remark: rename your class to Customer. A class represents a single object and should have a singular noun. Except for a collection-class (Customers would be a class representing a collection of customers). Same goes for your Order-class. Commented Dec 10, 2013 at 9:41

2 Answers 2

4

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");
Sign up to request clarification or add additional context in comments.

Comments

0

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

        YouList.Remove (Orders);

or YouList.RemoveAt(index)

2 Comments

Arrays do not have a Remove, nor a RemoveAt method.
yeah, but i forgot my info above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.