Question

Using Code if a condition is met to search for a similar product code, by trimstart, and then call a method to add that product to inventory list. Unfortunately it is causing a double addition so that the product is added twice.

If I use only the line of code for the List output to a GridView it displays one entry normally.

if (item.Name.StartsWith("D"))
{
    string name = item.Name.TrimStart('D');
    List<Item> dvd = items.SelectByName(name);
    foreach (Item item2 in dvd)
    {
        Class.AddItem(item2.Id, item2.Id2, item2.Name);
    }
}

Pas de solution correcte

Autres conseils

Can you check in your foreach, right before you AddItem, if the item already exisits in that class? If no, add, if yes, continue.

Here is some pseudo that you could try to add some validation before adding the item to the list:

if !the_list.Contains(the_name)
   the_list.Add(item)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top