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

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?
GouthamAnandAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BinuthCommented:
use

break;  keyword inside the loop
0
Pratima PharandeCommented:
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
CuteBugCommented:
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
CuteBugCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GouthamAnandAuthor Commented:
Thank you...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.