Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6326091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:01:13+00:00 2026-05-24T17:01:13+00:00

Possible Duplicates: Breaking out of a nested loop How to break out of 2

  • 0

Possible Duplicates:
Breaking out of a nested loop
How to break out of 2 loops without a flag variable in C#?

Hello I have a function that has nested loops. Once the condition has been met, I want to break out of nested loops. The code is something like this below:

foreach (EmpowerTaxView taxView in taxViews)
            {
                foreach (PayrollEmployee payrollEmployee in payrollEmployees)
                {
                    //PayStub payStub = payrollEmployee.InternalPayStub;

                    IReadOnlyList<PayrollWorkLocation> payrollWorkLocations =  payrollEmployee.PayrollWorkLocations;

                    foreach (PayrollWorkLocation payrollWorkLocation in payrollWorkLocations)
                    {
                        Tax tax = GetTaxEntity(payrollWorkLocation, taxView.BSITypeCode, taxView.BSIAuthorityCode,
                                               paidbyEr, resCode);

                        if (tax != null && tax.Rate.HasValue)
                        {
                            taxRate = tax.Rate.Value;
                            break;
                        }
                    }
                }
            }

Unfortunately, break comes out of only one loop. I want to break out of the whole thing. Please, I know some people have suggested goto: statement. I am wondering is there any other way around, such writing some LINQ queries to the same effect.

Any ideas and suggestions are greatly appreciated !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:01:16+00:00Added an answer on May 24, 2026 at 5:01 pm

    Two options suggest themselves as ways of getting out without having an extra flag variable to indicate “you should break out of the inner loop too”. (I really dislike having such variables, personally.

    One option is to pull all of this code into a separate method – then you can just return from the method. This would probably improve your code readability anyway – this really feels like it’s doing enough to warrant extracting into a separate method.

    The other obvious option is to use LINQ. Here’s an example which I think would work:

    var taxRate = (from taxView in taxViews
                   from employee in payrollEmployees
                   from location in employee.PayrollWorkLocations
                   let tax = GetTaxEntity(location, taxView.BSITypeCode,
                                          taxView.BSIAuthorityCode,
                                          paidbyEr, resCode)
                   where tax != null && tax.Rate.HasValue
                   select tax.Rate).FirstOrDefault();
    

    That looks considerably cleaner than lots of foreach loops to me.

    Note that I haven’t selected tax.Rate.Value – just tax.Rate. That means the result will be a “null” decimal? (or whatever type tax.Rate is) if no matching rates are found, or the rate otherwise. So you’d then have:

    if (taxRate != null)
    {
        // Use taxRate.Value here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Breaking out of a nested loop I have this code foreach (___)
Possible Duplicate: Breaking out of a nested loop How to exit from nested loops
Possible Duplicate: Breaking out of a nested loop I got somthing like (to simplify)
Possible Duplicates: Changing c++ output without changing the main() function How to assign a
Possible Duplicates: Is there a problem that has only a recursive solution? Can every
Possible Duplicates: What is the difference between $(document).ready(function() and $(function() ? What is the
Lets assume i have a function that takes 32bit integer in, and returns random
Possible Duplicates: Downloading a file in Delphi Delphi File Downloader Component Hello everyone, I'm
Possible Duplicates: Why does it appear that my random number generator isn't random in
Possible Duplicates: Is JavaScript's Math broken? Java floating point arithmetic I have the current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.