简体   繁体   中英

foreach loop does not loop through all item in list - C#

I have a basic foreach loop that calls a static method which makes a connection to a database and inserts some data. For some reason it will only iterate through the first item in the collection when I run the application without debugging. If I debug the application and set a break point on the foreach loop, it will iterate through all of the items in the collection.

If I set a break point and step over the foreach loop, it will demonstarte the same behavior as if I was running the application without debugging.

Does anyone know what would cause this type of behavior?

Here is a simplified version of the source code:

List<MyObject> objectlist = new List<MyObject>();

//some code to populate list

foreach(MyObject myobject in objectlist)
{
    string a = "a";
    string b = "b";

    MyLibrary.UpdateDatabase(a, b);
}

(I am using Visual Studio 2008 SP1)

The process does not throw any exceptions with or without debugging the application.

My guess would be that your code might be behaving differently when you give it more time by stepping through each line. (Presumably because of the database)

Make sure that the method isn't throwing any exceptions (put a catch block that calls Console.WriteLine or MessageBox.Show and see if anything happens).

Look in the database logs and see if there's anything interesting there.

Also, please post the full source of the method.

Normally when there is a difference between code running normally and code running in debug, it is related to the security context.

Code running in a process will run in the security context of that process. Code running in debug mode will run in the security context of the user doing the debugging.

The call to the database probably fails when the code is run normally, due to lack of rights. It would then appear that the loop only runs once.

It was not iterating through the foreach loop when I was not debugging the application because the myobject object was not used in the UpdateDatabase method call.

My source code should look like this:

List<MyObject> objectlist = new List<MyObject>();

//some code to populate list

foreach(MyObject myobject in objectlist)
{
    MyLibrary.UpdateDatabase(myobject.a, myobject.b);
}

For me it sounds like an exception. Just to be sure, did you checked everything in Debug - Exceptions to On?

I had the same problem. (The foreach loop was returning the first item...) I simply solved it by changing the 'foreach' loop by a 'for' loop and used the [i] to check every index in the list (that forced the loop to go through the list by every item)..

In my case, I had a list of objects of type Boat. (with boatType, cost, remark. etc) And I wanted to add only new boatTypes that don't exist.

this is the original code that didn't work:

//data access layer
DALInventory dalGear = new DALInventory();

public bool AddBoat(Boat boat)
{
     foreach (var item in AllBoats())
                {
                    if (item.BoatType == gear.BoatType)
                    {
                        //if boatType already exists
                        return false;
                    }
                }
                //if boatType doesn't exist
                dalGear.AddBoat(gear);
                return true;
}

This is the code (for loop) that solved my issue:

for (int i = 0; i < AllBoats().Count; i++)
                {
                    Boat b = new Boat();

                    b.BoatType = AllBoats()[i].BoatType;

                    if (b.BoatType == gear.BoatType)
                    {
                        return false;
                    }
                }

                dalGear.AddBoat(gear);
                return true;

I still don't understand why the first code (foreach loop) doesn't work, even though it seems to be right !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM