0

I just want to retrieve a particular InventoryID and with the help of InventoryID, I want to remove specific list members that match the InventoryID. For example, if the input is something like "INPOL". I want check this Id with arraylist and remove id,name,unit price and quantity that matches the input given by user("INPOL");

    {
        List<Inventory> inventories = new List<Inventory>();
        inventories.Add(new Inventory { InventoryId = "INDHS", InventoryName = "Office Chair", InventoryUnitPrice = 51, InventoryQty = 25 });
        inventories.Add(new Inventory { InventoryId = "INCDS", InventoryName = "Beds", InventoryUnitPrice = 105, InventoryQty = 12 });
        inventories.Add(new Inventory { InventoryId = "INING", InventoryName = "Dinning Tables", InventoryUnitPrice = 23, InventoryQty = 10 });
        inventories.Add(new Inventory { InventoryId = "INOPL", InventoryName = "Desks", InventoryUnitPrice = 155, InventoryQty = 7 });
        inventories.Add(new Inventory { InventoryId = "INWSZ", InventoryName = "Book Cases", InventoryUnitPrice = 80, InventoryQty = 34 });
        inventories.Add(new Inventory { InventoryId = "INQAB", InventoryName = "Coffee Tables", InventoryUnitPrice = 30, InventoryQty = 50 });

        return inventories;


    }
public void removeInventory(List<Inventory> inventories){
        Console.Write("Enter Inventory Id: ");
        InventoryId = Console.ReadLine();
        foreach(Inventory inventory in inventories){
            inventory.getId(InventoryId,inventories);
        
        }

    }
    public void getId(string? inventoryID, List<Inventory> inventories){
        if(inventoryID == InventoryId){
            inventories.Clear();
        }
    }```

        
        

Kokila B
  • 11
  • 2

1 Answers1

0

As I can see, you use List<T>, not obsolete ArrayList; in case of List<T> you can try RemoveAll:

string InventoryID = "INOPL";

...

inventories.RemoveAll(item => item != null && item.InventoryId == InventoryID);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Thank you so much, sir. It resolves my issues. I look forward to learning more from you, sir. If I have any doubts in the future, kindly help me with them, sir. – Kokila B Oct 21 '22 at 03:32