문제

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);
    }
}

올바른 솔루션이 없습니다

다른 팁

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top