• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 866
  • Last Modified:

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?
0
GouthamAnand
Asked:
GouthamAnand
1 Solution
 
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
 
GouthamAnandAuthor Commented:
Thank you...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now