?
Solved

How to come out of foreach loop, when it is there inside of couple of foreach loops?

Posted on 2008-11-19
5
Medium Priority
?
860 Views
Last Modified: 2012-05-05
I have foreach loops as shown below.
foreach (InvoiceBO objInvoice in objEDIBO.InvoiceList)
            {
             // loop 1  
              foreach (InvoiceLineItemBO lineItem in objInvoice.LineItemDetailsList)
                {
                   // loop 2
                    foreach(ItemChargeDetailsBO chargeDetails in lineItem.ChargeDetailsList)
                    {
                     // loop 3
                    }
                }
            }

How to come out of only 3rd foreach loop but need to continue with other 2 foreach loops?

How to come out of 2nd foreach when it is inside of 3rd foreach loop, but should continue with the 1st foreach loop?

How to come out of all 3 foreach loops but should continue in within the same procedure?

Can anyone help me in this regard?
0
Comment
Question by:GouthamAnand
5 Comments
 
LVL 14

Expert Comment

by:Binuth
ID: 22993260
use

break;  keyword inside the loop
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 22993301
Try this

 lab1: ;
            foreach (InvoiceBO objInvoice in objEDIBO.InvoiceList)
            {
                // loop 1  
                lab2: ;
                foreach (InvoiceLineItemBO lineItem in objInvoice.LineItemDetailsList)
                {
                    // loop 2
                     lab3: ;
                    foreach (ItemChargeDetailsBO chargeDetails in lineItem.ChargeDetailsList)
                    {
                       goto  lab1;
                    }
                }
            }

Change goto  lab1; as per your requirement
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 22993398
Try this code.

Set the appropriate value of variable 'brkType' to break out of the appropriate code.
enum BreakType
{
    LOOP1,
    LOOP2,
    LOOP3,
    NONE
}
 
BreakType brkType = BreakType.NONE;
 
foreach (InvoiceBO objInvoice in objEDIBO.InvoiceList)
{
 // loop 1  
  foreach (InvoiceLineItemBO lineItem in objInvoice.LineItemDetailsList)
    {
       // loop 2
        foreach(ItemChargeDetailsBO chargeDetails in lineItem.ChargeDetailsList)
        {
	    //loop 3
            // To Break out of the respective loop set the appropriate value here
	    brkType = BreakType.LOOP3;
	    break;
        }
	
        if (brkType == BreakType.LOOP2)
			break;
    }
    if (brkType == LOOP1)
		break;
}

Open in new window

0
 
LVL 16

Accepted Solution

by:
CuteBug earned 2000 total points
ID: 22993969
A small modification in the code

enum BreakType
{
    LOOP1,
    LOOP2,
    LOOP3,
    NONE
}
 
BreakType brkType = BreakType.NONE;
 
foreach (InvoiceBO objInvoice in objEDIBO.InvoiceList)
{
 // loop 1  
  foreach (InvoiceLineItemBO lineItem in objInvoice.LineItemDetailsList)
    {
       // loop 2
        foreach(ItemChargeDetailsBO chargeDetails in lineItem.ChargeDetailsList)
        {
            //loop 3
            // To Break out of the respective loop set the appropriate value here
            brkType = BreakType.LOOP3;
            break;
        }
        
        if ((brkType == BreakType.LOOP2) || (brkType == BreakType.LOOP1))
                        break;
    }
    if (brkType == BreakType.LOOP1)
                break;
}

Open in new window

0
 

Author Closing Comment

by:GouthamAnand
ID: 31518173
Thank you...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question