Search code examples
c#console-application

Delete() & Update() function in c# without using LINQ


So I have written a code for a basic inventory system, there is a basic mistake in my code on updating, the complete items in the list is updating rather than the preferred id which is passed as a parameter, and the delete function shows the error of "Collection was modified; enumeration operation may not execute." Im still a beginner

//This is my update method
    public void Update(int prodID)
    {
        if (prodID <= ProductList.Count)
        {
            foreach (Product p in ProductList)
            {
                if (ProductList.Exists(p => p.ProductId == prodID))
                {
                    Console.WriteLine("Product Name:");
                    string Name = Console.ReadLine();
                    Console.WriteLine("Product No.:");
                    string Productno = Console.ReadLine();
                    Console.WriteLine("Price:");
                    decimal Price = int.Parse(Console.ReadLine());
                    p.ProductName = Name;
                    p.ProductNo = Productno;
                    p.ListPrice = Price;
                }
            }
        }
        else
        {
            Console.WriteLine("ENTER A VALID PRODUCT ID\n");
        }
    }


//This is my Delete method
public void Delete(int prodID)
    { 
        if (prodID <= ProductList.Count)
        {
            foreach (Product p in ProductList)
            {                    
                if (ProductList.Exists(p => p.ProductId == prodID))
                {
                    ProductList.RemoveAt(prodID - 1);
                    Console.WriteLine("THE PRODUCT IS SUCCESSFULLY DELETED.\n");
                }    
            }
        }
        else
        {
            Console.WriteLine("ENTER A VALID PRODUCT ID\n");
        }           
    }

Solution

  • For the 2nd part too.

    Try to add .ToList() after foreach (Product p in ProductList)

    public void Delete(int prodID)
        { 
            if (prodID <= ProductList.Count)
            {
                foreach (Product p in ProductList.ToList())
                {                    
                    if (ProductList.Exists(p => p.ProductId == prodID))
                    {
                        ProductList.RemoveAt(prodID - 1);
                        Console.WriteLine("THE PRODUCT IS SUCCESSFULLY DELETED.\n");
                    }    
                }
            }
            else
            {
                Console.WriteLine("ENTER A VALID PRODUCT ID\n");
            }           
        }
    

    If your calling toList() a separated list will be "generated" so the foreach uses another list than the "original" ProductList. That means you can edit and delete the items in the ProductList without any problems.

    Hopefully, that's working for you.


    Code optimization

    You can also try to use .Select and .Where for your list to select specific items. I don't know your code but normally you can select specific items in a list of objects like this.

    public void Update(int prodID)
        {
            var product = ProductList.Where(p => p.ProductId == prodID).FirstOrDefault();
            if (product != null) {
                Console.WriteLine("Product Name:");
                string Name = Console.ReadLine();
                Console.WriteLine("Product No.:");
                string Productno = Console.ReadLine();
                Console.WriteLine("Price:");
                decimal Price = int.Parse(Console.ReadLine());
                p.ProductName = Name;
                p.ProductNo = Productno;
                p.ListPrice = Price;
            }
            else
            {
                Console.WriteLine("ENTER A VALID PRODUCT ID\n");
            }
        }