How to optimize a nested foreach loop which is creating same data object (with different values)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I have following code which gets MastrSegments from repository(entity framework). It's working but I think I am violating DRY (though not sure).



MastrSegments conatins collection of SegReqs and SegReq contains collection Routes. And I am looping through them to update RootObject



 foreach (var MastrSegment in MastrSegments)


var segReqCount = MastrSegment.segReqs.Count();

var MastrSegmentRow = new Row

c = new RootObject new RootObject v = MastrSegment.Name , new RootObject v = "TopClient" , new RootObject v = segReqCount , new RootObject v = 12

;

foreach (var segReq in MastrSegment.segReqs)


var routesCount = segReq.routess.Count;
Row segReqRow;

if (segReq.Name != MastrSegment.Name)

segReqRow = new Row

c = new RootObject new RootObject v = segReq.Name , new RootObject v = MastrSegment.Name , new RootObject v = routesCount , new RootObject v = -26

;

else

throw new NotSupportedException("MastrSegment name and segReq name can not be same !!");



foreach (var routes in segReq.routess)

var circuitsCount = routes.Circuits.Count;
Row routesRow = null;

if (routes.Name != segReq.Name)

routesRow = new Row

c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

;

else

throw new NotSupportedException("segReq name and routes name can not be same !!");


rows.Add(routesRow);


rows.Add(segReqRow);



rows.Add(MastrSegmentRow);


var returnData = rows.ToArray();






share|improve this question

























    up vote
    0
    down vote

    favorite












    I have following code which gets MastrSegments from repository(entity framework). It's working but I think I am violating DRY (though not sure).



    MastrSegments conatins collection of SegReqs and SegReq contains collection Routes. And I am looping through them to update RootObject



     foreach (var MastrSegment in MastrSegments)


    var segReqCount = MastrSegment.segReqs.Count();

    var MastrSegmentRow = new Row

    c = new RootObject new RootObject v = MastrSegment.Name , new RootObject v = "TopClient" , new RootObject v = segReqCount , new RootObject v = 12

    ;

    foreach (var segReq in MastrSegment.segReqs)


    var routesCount = segReq.routess.Count;
    Row segReqRow;

    if (segReq.Name != MastrSegment.Name)

    segReqRow = new Row

    c = new RootObject new RootObject v = segReq.Name , new RootObject v = MastrSegment.Name , new RootObject v = routesCount , new RootObject v = -26

    ;

    else

    throw new NotSupportedException("MastrSegment name and segReq name can not be same !!");



    foreach (var routes in segReq.routess)

    var circuitsCount = routes.Circuits.Count;
    Row routesRow = null;

    if (routes.Name != segReq.Name)

    routesRow = new Row

    c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

    ;

    else

    throw new NotSupportedException("segReq name and routes name can not be same !!");


    rows.Add(routesRow);


    rows.Add(segReqRow);



    rows.Add(MastrSegmentRow);


    var returnData = rows.ToArray();






    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have following code which gets MastrSegments from repository(entity framework). It's working but I think I am violating DRY (though not sure).



      MastrSegments conatins collection of SegReqs and SegReq contains collection Routes. And I am looping through them to update RootObject



       foreach (var MastrSegment in MastrSegments)


      var segReqCount = MastrSegment.segReqs.Count();

      var MastrSegmentRow = new Row

      c = new RootObject new RootObject v = MastrSegment.Name , new RootObject v = "TopClient" , new RootObject v = segReqCount , new RootObject v = 12

      ;

      foreach (var segReq in MastrSegment.segReqs)


      var routesCount = segReq.routess.Count;
      Row segReqRow;

      if (segReq.Name != MastrSegment.Name)

      segReqRow = new Row

      c = new RootObject new RootObject v = segReq.Name , new RootObject v = MastrSegment.Name , new RootObject v = routesCount , new RootObject v = -26

      ;

      else

      throw new NotSupportedException("MastrSegment name and segReq name can not be same !!");



      foreach (var routes in segReq.routess)

      var circuitsCount = routes.Circuits.Count;
      Row routesRow = null;

      if (routes.Name != segReq.Name)

      routesRow = new Row

      c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

      ;

      else

      throw new NotSupportedException("segReq name and routes name can not be same !!");


      rows.Add(routesRow);


      rows.Add(segReqRow);



      rows.Add(MastrSegmentRow);


      var returnData = rows.ToArray();






      share|improve this question











      I have following code which gets MastrSegments from repository(entity framework). It's working but I think I am violating DRY (though not sure).



      MastrSegments conatins collection of SegReqs and SegReq contains collection Routes. And I am looping through them to update RootObject



       foreach (var MastrSegment in MastrSegments)


      var segReqCount = MastrSegment.segReqs.Count();

      var MastrSegmentRow = new Row

      c = new RootObject new RootObject v = MastrSegment.Name , new RootObject v = "TopClient" , new RootObject v = segReqCount , new RootObject v = 12

      ;

      foreach (var segReq in MastrSegment.segReqs)


      var routesCount = segReq.routess.Count;
      Row segReqRow;

      if (segReq.Name != MastrSegment.Name)

      segReqRow = new Row

      c = new RootObject new RootObject v = segReq.Name , new RootObject v = MastrSegment.Name , new RootObject v = routesCount , new RootObject v = -26

      ;

      else

      throw new NotSupportedException("MastrSegment name and segReq name can not be same !!");



      foreach (var routes in segReq.routess)

      var circuitsCount = routes.Circuits.Count;
      Row routesRow = null;

      if (routes.Name != segReq.Name)

      routesRow = new Row

      c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

      ;

      else

      throw new NotSupportedException("segReq name and routes name can not be same !!");


      rows.Add(routesRow);


      rows.Add(segReqRow);



      rows.Add(MastrSegmentRow);


      var returnData = rows.ToArray();








      share|improve this question










      share|improve this question




      share|improve this question









      asked Jan 24 at 9:58









      Simsons

      3931314




      3931314




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          when ever you want to solve violations of the DRY principle you have to convert similar code into equal code.



          If you look at the two inner loops: make the most inner loop using new local variables to replace the differeing parts




           
          var circuitsCount = routes.Circuits.Count;
          Row routesRow = null;

          if (routes.Name != segReq.Name)

          routesRow = new Row

          c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

          ;

          else

          throw new NotSupportedException("segReq name and routes name can not be same !!");


          rows.Add(routesRow);




          becomes



           // may be no valid syntax since I'm not a C# guy...
          var count = routes.Circuits.Count;
          Row routesRow = null;
          var master = "routes"
          var masterName = routes.Name;
          var detail = "segReq"
          var detailName = segReq.Name;
          var magicNumber = 16;

          if (masterName != detailName )

          routesRow = new Row

          c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = count , new RootObject v = magicNumber

          ;

          else

          throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");


          rows.Add(routesRow);



          now you extract the "future common" part to a new method:



           
          var count = routes.Circuits.Count;
          var master = "routes"
          var masterName = routes.Name;
          var detail = "segReq"
          var detailName = segReq.Name;
          var = magicNumber 16;

          Row routesRow = CreateMasterDetailRow(master, masterName, detail, detailName, count, magicNumber);
          rows.Add(routesRow);

          //...

          func Row CreateMasterDetailRow(var master, var masterName, var detail, var detailName, var count, var magicNumber)


          if (masterName != detailName )

          return new Row

          c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = circuitsCount , new RootObject v = magicNumber

          ;

          else

          throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");




          inline the extracted variables into the function call:



           
          Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
          rows.Add(routesRow);



          then use the call in the middle loop:



           { // maybe I got the order of "master" and "detail" wrong...
          Row segReqRow= CreateMasterDetailRow("MastrSegment ", MastrSegment.Name, "segReq", segReq.Name, segReq.routess.Count, -26);
          foreach (var routes in segReq.routess)

          Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
          rows.Add(routesRow);

          rows.Add(segReqRow);


          You can even replace the similar code in the outer loop if you provide some "fake data" for the (not failing) consistency check to satify the new methods parameter list...






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185864%2fhow-to-optimize-a-nested-foreach-loop-which-is-creating-same-data-object-with-d%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            when ever you want to solve violations of the DRY principle you have to convert similar code into equal code.



            If you look at the two inner loops: make the most inner loop using new local variables to replace the differeing parts




             
            var circuitsCount = routes.Circuits.Count;
            Row routesRow = null;

            if (routes.Name != segReq.Name)

            routesRow = new Row

            c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

            ;

            else

            throw new NotSupportedException("segReq name and routes name can not be same !!");


            rows.Add(routesRow);




            becomes



             // may be no valid syntax since I'm not a C# guy...
            var count = routes.Circuits.Count;
            Row routesRow = null;
            var master = "routes"
            var masterName = routes.Name;
            var detail = "segReq"
            var detailName = segReq.Name;
            var magicNumber = 16;

            if (masterName != detailName )

            routesRow = new Row

            c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = count , new RootObject v = magicNumber

            ;

            else

            throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");


            rows.Add(routesRow);



            now you extract the "future common" part to a new method:



             
            var count = routes.Circuits.Count;
            var master = "routes"
            var masterName = routes.Name;
            var detail = "segReq"
            var detailName = segReq.Name;
            var = magicNumber 16;

            Row routesRow = CreateMasterDetailRow(master, masterName, detail, detailName, count, magicNumber);
            rows.Add(routesRow);

            //...

            func Row CreateMasterDetailRow(var master, var masterName, var detail, var detailName, var count, var magicNumber)


            if (masterName != detailName )

            return new Row

            c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = circuitsCount , new RootObject v = magicNumber

            ;

            else

            throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");




            inline the extracted variables into the function call:



             
            Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
            rows.Add(routesRow);



            then use the call in the middle loop:



             { // maybe I got the order of "master" and "detail" wrong...
            Row segReqRow= CreateMasterDetailRow("MastrSegment ", MastrSegment.Name, "segReq", segReq.Name, segReq.routess.Count, -26);
            foreach (var routes in segReq.routess)

            Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
            rows.Add(routesRow);

            rows.Add(segReqRow);


            You can even replace the similar code in the outer loop if you provide some "fake data" for the (not failing) consistency check to satify the new methods parameter list...






            share|improve this answer



























              up vote
              3
              down vote













              when ever you want to solve violations of the DRY principle you have to convert similar code into equal code.



              If you look at the two inner loops: make the most inner loop using new local variables to replace the differeing parts




               
              var circuitsCount = routes.Circuits.Count;
              Row routesRow = null;

              if (routes.Name != segReq.Name)

              routesRow = new Row

              c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

              ;

              else

              throw new NotSupportedException("segReq name and routes name can not be same !!");


              rows.Add(routesRow);




              becomes



               // may be no valid syntax since I'm not a C# guy...
              var count = routes.Circuits.Count;
              Row routesRow = null;
              var master = "routes"
              var masterName = routes.Name;
              var detail = "segReq"
              var detailName = segReq.Name;
              var magicNumber = 16;

              if (masterName != detailName )

              routesRow = new Row

              c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = count , new RootObject v = magicNumber

              ;

              else

              throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");


              rows.Add(routesRow);



              now you extract the "future common" part to a new method:



               
              var count = routes.Circuits.Count;
              var master = "routes"
              var masterName = routes.Name;
              var detail = "segReq"
              var detailName = segReq.Name;
              var = magicNumber 16;

              Row routesRow = CreateMasterDetailRow(master, masterName, detail, detailName, count, magicNumber);
              rows.Add(routesRow);

              //...

              func Row CreateMasterDetailRow(var master, var masterName, var detail, var detailName, var count, var magicNumber)


              if (masterName != detailName )

              return new Row

              c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = circuitsCount , new RootObject v = magicNumber

              ;

              else

              throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");




              inline the extracted variables into the function call:



               
              Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
              rows.Add(routesRow);



              then use the call in the middle loop:



               { // maybe I got the order of "master" and "detail" wrong...
              Row segReqRow= CreateMasterDetailRow("MastrSegment ", MastrSegment.Name, "segReq", segReq.Name, segReq.routess.Count, -26);
              foreach (var routes in segReq.routess)

              Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
              rows.Add(routesRow);

              rows.Add(segReqRow);


              You can even replace the similar code in the outer loop if you provide some "fake data" for the (not failing) consistency check to satify the new methods parameter list...






              share|improve this answer

























                up vote
                3
                down vote










                up vote
                3
                down vote









                when ever you want to solve violations of the DRY principle you have to convert similar code into equal code.



                If you look at the two inner loops: make the most inner loop using new local variables to replace the differeing parts




                 
                var circuitsCount = routes.Circuits.Count;
                Row routesRow = null;

                if (routes.Name != segReq.Name)

                routesRow = new Row

                c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

                ;

                else

                throw new NotSupportedException("segReq name and routes name can not be same !!");


                rows.Add(routesRow);




                becomes



                 // may be no valid syntax since I'm not a C# guy...
                var count = routes.Circuits.Count;
                Row routesRow = null;
                var master = "routes"
                var masterName = routes.Name;
                var detail = "segReq"
                var detailName = segReq.Name;
                var magicNumber = 16;

                if (masterName != detailName )

                routesRow = new Row

                c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = count , new RootObject v = magicNumber

                ;

                else

                throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");


                rows.Add(routesRow);



                now you extract the "future common" part to a new method:



                 
                var count = routes.Circuits.Count;
                var master = "routes"
                var masterName = routes.Name;
                var detail = "segReq"
                var detailName = segReq.Name;
                var = magicNumber 16;

                Row routesRow = CreateMasterDetailRow(master, masterName, detail, detailName, count, magicNumber);
                rows.Add(routesRow);

                //...

                func Row CreateMasterDetailRow(var master, var masterName, var detail, var detailName, var count, var magicNumber)


                if (masterName != detailName )

                return new Row

                c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = circuitsCount , new RootObject v = magicNumber

                ;

                else

                throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");




                inline the extracted variables into the function call:



                 
                Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
                rows.Add(routesRow);



                then use the call in the middle loop:



                 { // maybe I got the order of "master" and "detail" wrong...
                Row segReqRow= CreateMasterDetailRow("MastrSegment ", MastrSegment.Name, "segReq", segReq.Name, segReq.routess.Count, -26);
                foreach (var routes in segReq.routess)

                Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
                rows.Add(routesRow);

                rows.Add(segReqRow);


                You can even replace the similar code in the outer loop if you provide some "fake data" for the (not failing) consistency check to satify the new methods parameter list...






                share|improve this answer















                when ever you want to solve violations of the DRY principle you have to convert similar code into equal code.



                If you look at the two inner loops: make the most inner loop using new local variables to replace the differeing parts




                 
                var circuitsCount = routes.Circuits.Count;
                Row routesRow = null;

                if (routes.Name != segReq.Name)

                routesRow = new Row

                c = new RootObject new RootObject v = routes.Name , new RootObject v = segReq.Name , new RootObject v = circuitsCount , new RootObject v = 16

                ;

                else

                throw new NotSupportedException("segReq name and routes name can not be same !!");


                rows.Add(routesRow);




                becomes



                 // may be no valid syntax since I'm not a C# guy...
                var count = routes.Circuits.Count;
                Row routesRow = null;
                var master = "routes"
                var masterName = routes.Name;
                var detail = "segReq"
                var detailName = segReq.Name;
                var magicNumber = 16;

                if (masterName != detailName )

                routesRow = new Row

                c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = count , new RootObject v = magicNumber

                ;

                else

                throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");


                rows.Add(routesRow);



                now you extract the "future common" part to a new method:



                 
                var count = routes.Circuits.Count;
                var master = "routes"
                var masterName = routes.Name;
                var detail = "segReq"
                var detailName = segReq.Name;
                var = magicNumber 16;

                Row routesRow = CreateMasterDetailRow(master, masterName, detail, detailName, count, magicNumber);
                rows.Add(routesRow);

                //...

                func Row CreateMasterDetailRow(var master, var masterName, var detail, var detailName, var count, var magicNumber)


                if (masterName != detailName )

                return new Row

                c = new RootObject new RootObject v = masterName , new RootObject v = detailName , new RootObject v = circuitsCount , new RootObject v = magicNumber

                ;

                else

                throw new NotSupportedException(detail+" name and "+master+" name can not be same !!");




                inline the extracted variables into the function call:



                 
                Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
                rows.Add(routesRow);



                then use the call in the middle loop:



                 { // maybe I got the order of "master" and "detail" wrong...
                Row segReqRow= CreateMasterDetailRow("MastrSegment ", MastrSegment.Name, "segReq", segReq.Name, segReq.routess.Count, -26);
                foreach (var routes in segReq.routess)

                Row routesRow = CreateMasterDetailRow("routes", routes.Name, "segReq", segReq.Name, routes.Circuits.Count, 16);
                rows.Add(routesRow);

                rows.Add(segReqRow);


                You can even replace the similar code in the outer loop if you provide some "fake data" for the (not failing) consistency check to satify the new methods parameter list...







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jan 24 at 10:45


























                answered Jan 24 at 10:35









                Timothy Truckle

                4,673316




                4,673316






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185864%2fhow-to-optimize-a-nested-foreach-loop-which-is-creating-same-data-object-with-d%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?