IEnumerable Best Practice of removing int item

I have a IEmunerable list with N items for example: 23, 1, 38.....

The needed logic is, when looping thru the list:

1: find if 1exist

2: if 1 exist, find 2

3: if 2 exist, find 3

4: If 3 exist, remove 3 from the current list.

My current approach is:

foreach(var x in someIntList)
{
    if(x==1)
    {
        if(someIntList.Any(y => y==2))
        {
            if(someIntList.Any(z => z==3))
            {
                //This is the shortest code i can think of, but apparently its wrong. Error saying there is no Except method for IEmunerable<Int> ?
                someIntList = someIntList.Except(3);

            }

        }

    }   

}
Jon Skeet
people
quotationmark

It's not clear why you're looping to start with, or using Any instead of Contains:

if (someIntList.Contains(1) && someIntList.Contains(2) && someIntList.Contains(3))
{
    someIntList = someIntList.Where(x => x != 3); // Possibly with ToList()?
}

You probably don't want to use Except as that's a set-based operation - if your original list contains duplicates, they will be removed if you use Except.

Note that this will remove all occurrences of 3 - is that what you want, or do you just want to remove the first occurrence of 3?

people

See more on this question at Stackoverflow