How to fetch a list of internal members from outer list?












0















I have a Class something similar to like below



class OuterClass
{
public InnerClass InnerObject{get;set;}
////Some more properties and methods
}


Now in another class I have two lists, one of OuterClass objects and one of InnerClass objects. And I want the list of InnerClass objects to be populated from the property InnerObject of objects in OuterClass list.
I can achieve this by iterating through OuterClass list and populating the InnerClass list. However, this needs to be taken care anytime OuterClass list is modified.



class AnotherClass
{
private List<OuterClass> OuterClassList{get;set;}
private List<InnerClass> InnerClassList{get;set;}

public void InsertOuterClassObject(OuterClass outerClassObject)
{
this.OuterClassList.Add(outerClassObject);
this.PopulateInnerClassList();
}

private void PopulateInnerClassList()
{
this.InnerClassList = new List<InnerClass>();
foreach(var object in this.OuterClassList)
{
this.InnerClassList.Add(object.InnerObject);
}
}

////More operations to be performed on OuterClassList
}


Is there some other way I can make sure that the InnerClassList is correctly synced with OuterClassList rather than having to iterate and populate and also to make sure the method PopulateInnerClassList() is surely called when necessary.










share|improve this question



























    0















    I have a Class something similar to like below



    class OuterClass
    {
    public InnerClass InnerObject{get;set;}
    ////Some more properties and methods
    }


    Now in another class I have two lists, one of OuterClass objects and one of InnerClass objects. And I want the list of InnerClass objects to be populated from the property InnerObject of objects in OuterClass list.
    I can achieve this by iterating through OuterClass list and populating the InnerClass list. However, this needs to be taken care anytime OuterClass list is modified.



    class AnotherClass
    {
    private List<OuterClass> OuterClassList{get;set;}
    private List<InnerClass> InnerClassList{get;set;}

    public void InsertOuterClassObject(OuterClass outerClassObject)
    {
    this.OuterClassList.Add(outerClassObject);
    this.PopulateInnerClassList();
    }

    private void PopulateInnerClassList()
    {
    this.InnerClassList = new List<InnerClass>();
    foreach(var object in this.OuterClassList)
    {
    this.InnerClassList.Add(object.InnerObject);
    }
    }

    ////More operations to be performed on OuterClassList
    }


    Is there some other way I can make sure that the InnerClassList is correctly synced with OuterClassList rather than having to iterate and populate and also to make sure the method PopulateInnerClassList() is surely called when necessary.










    share|improve this question

























      0












      0








      0








      I have a Class something similar to like below



      class OuterClass
      {
      public InnerClass InnerObject{get;set;}
      ////Some more properties and methods
      }


      Now in another class I have two lists, one of OuterClass objects and one of InnerClass objects. And I want the list of InnerClass objects to be populated from the property InnerObject of objects in OuterClass list.
      I can achieve this by iterating through OuterClass list and populating the InnerClass list. However, this needs to be taken care anytime OuterClass list is modified.



      class AnotherClass
      {
      private List<OuterClass> OuterClassList{get;set;}
      private List<InnerClass> InnerClassList{get;set;}

      public void InsertOuterClassObject(OuterClass outerClassObject)
      {
      this.OuterClassList.Add(outerClassObject);
      this.PopulateInnerClassList();
      }

      private void PopulateInnerClassList()
      {
      this.InnerClassList = new List<InnerClass>();
      foreach(var object in this.OuterClassList)
      {
      this.InnerClassList.Add(object.InnerObject);
      }
      }

      ////More operations to be performed on OuterClassList
      }


      Is there some other way I can make sure that the InnerClassList is correctly synced with OuterClassList rather than having to iterate and populate and also to make sure the method PopulateInnerClassList() is surely called when necessary.










      share|improve this question














      I have a Class something similar to like below



      class OuterClass
      {
      public InnerClass InnerObject{get;set;}
      ////Some more properties and methods
      }


      Now in another class I have two lists, one of OuterClass objects and one of InnerClass objects. And I want the list of InnerClass objects to be populated from the property InnerObject of objects in OuterClass list.
      I can achieve this by iterating through OuterClass list and populating the InnerClass list. However, this needs to be taken care anytime OuterClass list is modified.



      class AnotherClass
      {
      private List<OuterClass> OuterClassList{get;set;}
      private List<InnerClass> InnerClassList{get;set;}

      public void InsertOuterClassObject(OuterClass outerClassObject)
      {
      this.OuterClassList.Add(outerClassObject);
      this.PopulateInnerClassList();
      }

      private void PopulateInnerClassList()
      {
      this.InnerClassList = new List<InnerClass>();
      foreach(var object in this.OuterClassList)
      {
      this.InnerClassList.Add(object.InnerObject);
      }
      }

      ////More operations to be performed on OuterClassList
      }


      Is there some other way I can make sure that the InnerClassList is correctly synced with OuterClassList rather than having to iterate and populate and also to make sure the method PopulateInnerClassList() is surely called when necessary.







      c# .net list






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 4:54









      No nameNo name

      12111




      12111
























          3 Answers
          3






          active

          oldest

          votes


















          1














          The sample code suggests the InnerClassList should only reflect state of the OuterClassList and not be maintained independently. If this is the case, you may build the inner list on the fly:



          private List<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject).ToList();


          And if InnerClassList's usage pattern is limited to iterating through it, you may change its type to IEnumerable<InnerClass> thus avoiding performance degradation on long lists:



          private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);


          So the resulting list maintenance code may look like this:



          class AnotherClass
          {
          private List<OuterClass> OuterClassList { get; } = new List<OuterClass>();

          private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);

          public void InsertOuterClassObject(OuterClass outerClassObject)
          {
          this.OuterClassList.Add(outerClassObject);
          }

          ////More operations to be performed on OuterClassList
          }


          Note: I also changed definition of OuterClassList to ensure it's always instantiated. Granted it's a private property, changing it to a read-only once-initialized object won't break AnotherClass API and make internal manipulation easier.






          share|improve this answer
























          • Just don't ever call InnerClassList.Add(item) and expect it to do anything.

            – John Wu
            Jan 2 at 6:55



















          1














          Try to see if it meets your needs.



          public class AnotherClass
          {
          private List<OuterClass> _OuterClassList;
          private List<InnerClass> _InnerClassList;
          private List<OuterClass> OuterClassList {
          get { return _OuterClassList; }
          set
          {
          _OuterClassList = value;
          InnerClassList = value.Select(a => a.InnerObject).ToList();
          }
          }
          private List<InnerClass> InnerClassList { get { return _InnerClassList; } set { InnerClassList = value; } }

          public void InsertOuterClassObject(OuterClass outerClassObject)
          {
          this.OuterClassList.Add(outerClassObject);
          this.PopulateInnerClassList();
          }

          private void PopulateInnerClassList()
          {
          this.InnerClassList = new List<InnerClass>();
          foreach (var item in this.OuterClassList)
          {
          this.InnerClassList.Add(item.InnerObject);
          }
          }
          }





          share|improve this answer































            1














            You don't need InnerClassList I think, look this:



            private List<OuterClass> OuterClassList { get; set; }//initialize in constructor.

            internal class OuterClass
            {
            public InnerClass InnerObject { get; set; }

            public OuterClass(int num) => InnerObject = new InnerClass(num);
            }

            internal class InnerClass
            {
            public int Value { get; set; }
            public InnerClass(int num) => Value = num;
            }

            private void button1_Click(object sender, EventArgs e) => OuterClassList.Add(new OuterClass(new Random().Next(0, 1000)));

            private void button4_Click(object sender, EventArgs e)
            {
            var values = from o in OuterClassList select o.InnerObject.Value;//you always have it, then you don't need InnerClassList I think.
            foreach (var value in values)
            MessageBox.Show(value.ToString());
            }





            share|improve this answer























              Your Answer






              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: "1"
              };
              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001373%2fhow-to-fetch-a-list-of-internal-members-from-outer-list%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              The sample code suggests the InnerClassList should only reflect state of the OuterClassList and not be maintained independently. If this is the case, you may build the inner list on the fly:



              private List<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject).ToList();


              And if InnerClassList's usage pattern is limited to iterating through it, you may change its type to IEnumerable<InnerClass> thus avoiding performance degradation on long lists:



              private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);


              So the resulting list maintenance code may look like this:



              class AnotherClass
              {
              private List<OuterClass> OuterClassList { get; } = new List<OuterClass>();

              private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);

              public void InsertOuterClassObject(OuterClass outerClassObject)
              {
              this.OuterClassList.Add(outerClassObject);
              }

              ////More operations to be performed on OuterClassList
              }


              Note: I also changed definition of OuterClassList to ensure it's always instantiated. Granted it's a private property, changing it to a read-only once-initialized object won't break AnotherClass API and make internal manipulation easier.






              share|improve this answer
























              • Just don't ever call InnerClassList.Add(item) and expect it to do anything.

                – John Wu
                Jan 2 at 6:55
















              1














              The sample code suggests the InnerClassList should only reflect state of the OuterClassList and not be maintained independently. If this is the case, you may build the inner list on the fly:



              private List<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject).ToList();


              And if InnerClassList's usage pattern is limited to iterating through it, you may change its type to IEnumerable<InnerClass> thus avoiding performance degradation on long lists:



              private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);


              So the resulting list maintenance code may look like this:



              class AnotherClass
              {
              private List<OuterClass> OuterClassList { get; } = new List<OuterClass>();

              private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);

              public void InsertOuterClassObject(OuterClass outerClassObject)
              {
              this.OuterClassList.Add(outerClassObject);
              }

              ////More operations to be performed on OuterClassList
              }


              Note: I also changed definition of OuterClassList to ensure it's always instantiated. Granted it's a private property, changing it to a read-only once-initialized object won't break AnotherClass API and make internal manipulation easier.






              share|improve this answer
























              • Just don't ever call InnerClassList.Add(item) and expect it to do anything.

                – John Wu
                Jan 2 at 6:55














              1












              1








              1







              The sample code suggests the InnerClassList should only reflect state of the OuterClassList and not be maintained independently. If this is the case, you may build the inner list on the fly:



              private List<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject).ToList();


              And if InnerClassList's usage pattern is limited to iterating through it, you may change its type to IEnumerable<InnerClass> thus avoiding performance degradation on long lists:



              private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);


              So the resulting list maintenance code may look like this:



              class AnotherClass
              {
              private List<OuterClass> OuterClassList { get; } = new List<OuterClass>();

              private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);

              public void InsertOuterClassObject(OuterClass outerClassObject)
              {
              this.OuterClassList.Add(outerClassObject);
              }

              ////More operations to be performed on OuterClassList
              }


              Note: I also changed definition of OuterClassList to ensure it's always instantiated. Granted it's a private property, changing it to a read-only once-initialized object won't break AnotherClass API and make internal manipulation easier.






              share|improve this answer













              The sample code suggests the InnerClassList should only reflect state of the OuterClassList and not be maintained independently. If this is the case, you may build the inner list on the fly:



              private List<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject).ToList();


              And if InnerClassList's usage pattern is limited to iterating through it, you may change its type to IEnumerable<InnerClass> thus avoiding performance degradation on long lists:



              private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);


              So the resulting list maintenance code may look like this:



              class AnotherClass
              {
              private List<OuterClass> OuterClassList { get; } = new List<OuterClass>();

              private IEnumerable<InnerClass> InnerClassList => OuterClassList.Select(x => x.InnerObject);

              public void InsertOuterClassObject(OuterClass outerClassObject)
              {
              this.OuterClassList.Add(outerClassObject);
              }

              ////More operations to be performed on OuterClassList
              }


              Note: I also changed definition of OuterClassList to ensure it's always instantiated. Granted it's a private property, changing it to a read-only once-initialized object won't break AnotherClass API and make internal manipulation easier.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 2 at 5:50









              Dmitry EgorovDmitry Egorov

              8,52131431




              8,52131431













              • Just don't ever call InnerClassList.Add(item) and expect it to do anything.

                – John Wu
                Jan 2 at 6:55



















              • Just don't ever call InnerClassList.Add(item) and expect it to do anything.

                – John Wu
                Jan 2 at 6:55

















              Just don't ever call InnerClassList.Add(item) and expect it to do anything.

              – John Wu
              Jan 2 at 6:55





              Just don't ever call InnerClassList.Add(item) and expect it to do anything.

              – John Wu
              Jan 2 at 6:55













              1














              Try to see if it meets your needs.



              public class AnotherClass
              {
              private List<OuterClass> _OuterClassList;
              private List<InnerClass> _InnerClassList;
              private List<OuterClass> OuterClassList {
              get { return _OuterClassList; }
              set
              {
              _OuterClassList = value;
              InnerClassList = value.Select(a => a.InnerObject).ToList();
              }
              }
              private List<InnerClass> InnerClassList { get { return _InnerClassList; } set { InnerClassList = value; } }

              public void InsertOuterClassObject(OuterClass outerClassObject)
              {
              this.OuterClassList.Add(outerClassObject);
              this.PopulateInnerClassList();
              }

              private void PopulateInnerClassList()
              {
              this.InnerClassList = new List<InnerClass>();
              foreach (var item in this.OuterClassList)
              {
              this.InnerClassList.Add(item.InnerObject);
              }
              }
              }





              share|improve this answer




























                1














                Try to see if it meets your needs.



                public class AnotherClass
                {
                private List<OuterClass> _OuterClassList;
                private List<InnerClass> _InnerClassList;
                private List<OuterClass> OuterClassList {
                get { return _OuterClassList; }
                set
                {
                _OuterClassList = value;
                InnerClassList = value.Select(a => a.InnerObject).ToList();
                }
                }
                private List<InnerClass> InnerClassList { get { return _InnerClassList; } set { InnerClassList = value; } }

                public void InsertOuterClassObject(OuterClass outerClassObject)
                {
                this.OuterClassList.Add(outerClassObject);
                this.PopulateInnerClassList();
                }

                private void PopulateInnerClassList()
                {
                this.InnerClassList = new List<InnerClass>();
                foreach (var item in this.OuterClassList)
                {
                this.InnerClassList.Add(item.InnerObject);
                }
                }
                }





                share|improve this answer


























                  1












                  1








                  1







                  Try to see if it meets your needs.



                  public class AnotherClass
                  {
                  private List<OuterClass> _OuterClassList;
                  private List<InnerClass> _InnerClassList;
                  private List<OuterClass> OuterClassList {
                  get { return _OuterClassList; }
                  set
                  {
                  _OuterClassList = value;
                  InnerClassList = value.Select(a => a.InnerObject).ToList();
                  }
                  }
                  private List<InnerClass> InnerClassList { get { return _InnerClassList; } set { InnerClassList = value; } }

                  public void InsertOuterClassObject(OuterClass outerClassObject)
                  {
                  this.OuterClassList.Add(outerClassObject);
                  this.PopulateInnerClassList();
                  }

                  private void PopulateInnerClassList()
                  {
                  this.InnerClassList = new List<InnerClass>();
                  foreach (var item in this.OuterClassList)
                  {
                  this.InnerClassList.Add(item.InnerObject);
                  }
                  }
                  }





                  share|improve this answer













                  Try to see if it meets your needs.



                  public class AnotherClass
                  {
                  private List<OuterClass> _OuterClassList;
                  private List<InnerClass> _InnerClassList;
                  private List<OuterClass> OuterClassList {
                  get { return _OuterClassList; }
                  set
                  {
                  _OuterClassList = value;
                  InnerClassList = value.Select(a => a.InnerObject).ToList();
                  }
                  }
                  private List<InnerClass> InnerClassList { get { return _InnerClassList; } set { InnerClassList = value; } }

                  public void InsertOuterClassObject(OuterClass outerClassObject)
                  {
                  this.OuterClassList.Add(outerClassObject);
                  this.PopulateInnerClassList();
                  }

                  private void PopulateInnerClassList()
                  {
                  this.InnerClassList = new List<InnerClass>();
                  foreach (var item in this.OuterClassList)
                  {
                  this.InnerClassList.Add(item.InnerObject);
                  }
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 5:18









                  StoneStone

                  611




                  611























                      1














                      You don't need InnerClassList I think, look this:



                      private List<OuterClass> OuterClassList { get; set; }//initialize in constructor.

                      internal class OuterClass
                      {
                      public InnerClass InnerObject { get; set; }

                      public OuterClass(int num) => InnerObject = new InnerClass(num);
                      }

                      internal class InnerClass
                      {
                      public int Value { get; set; }
                      public InnerClass(int num) => Value = num;
                      }

                      private void button1_Click(object sender, EventArgs e) => OuterClassList.Add(new OuterClass(new Random().Next(0, 1000)));

                      private void button4_Click(object sender, EventArgs e)
                      {
                      var values = from o in OuterClassList select o.InnerObject.Value;//you always have it, then you don't need InnerClassList I think.
                      foreach (var value in values)
                      MessageBox.Show(value.ToString());
                      }





                      share|improve this answer




























                        1














                        You don't need InnerClassList I think, look this:



                        private List<OuterClass> OuterClassList { get; set; }//initialize in constructor.

                        internal class OuterClass
                        {
                        public InnerClass InnerObject { get; set; }

                        public OuterClass(int num) => InnerObject = new InnerClass(num);
                        }

                        internal class InnerClass
                        {
                        public int Value { get; set; }
                        public InnerClass(int num) => Value = num;
                        }

                        private void button1_Click(object sender, EventArgs e) => OuterClassList.Add(new OuterClass(new Random().Next(0, 1000)));

                        private void button4_Click(object sender, EventArgs e)
                        {
                        var values = from o in OuterClassList select o.InnerObject.Value;//you always have it, then you don't need InnerClassList I think.
                        foreach (var value in values)
                        MessageBox.Show(value.ToString());
                        }





                        share|improve this answer


























                          1












                          1








                          1







                          You don't need InnerClassList I think, look this:



                          private List<OuterClass> OuterClassList { get; set; }//initialize in constructor.

                          internal class OuterClass
                          {
                          public InnerClass InnerObject { get; set; }

                          public OuterClass(int num) => InnerObject = new InnerClass(num);
                          }

                          internal class InnerClass
                          {
                          public int Value { get; set; }
                          public InnerClass(int num) => Value = num;
                          }

                          private void button1_Click(object sender, EventArgs e) => OuterClassList.Add(new OuterClass(new Random().Next(0, 1000)));

                          private void button4_Click(object sender, EventArgs e)
                          {
                          var values = from o in OuterClassList select o.InnerObject.Value;//you always have it, then you don't need InnerClassList I think.
                          foreach (var value in values)
                          MessageBox.Show(value.ToString());
                          }





                          share|improve this answer













                          You don't need InnerClassList I think, look this:



                          private List<OuterClass> OuterClassList { get; set; }//initialize in constructor.

                          internal class OuterClass
                          {
                          public InnerClass InnerObject { get; set; }

                          public OuterClass(int num) => InnerObject = new InnerClass(num);
                          }

                          internal class InnerClass
                          {
                          public int Value { get; set; }
                          public InnerClass(int num) => Value = num;
                          }

                          private void button1_Click(object sender, EventArgs e) => OuterClassList.Add(new OuterClass(new Random().Next(0, 1000)));

                          private void button4_Click(object sender, EventArgs e)
                          {
                          var values = from o in OuterClassList select o.InnerObject.Value;//you always have it, then you don't need InnerClassList I think.
                          foreach (var value in values)
                          MessageBox.Show(value.ToString());
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 2 at 5:19









                          CodeManCodeMan

                          557311




                          557311






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001373%2fhow-to-fetch-a-list-of-internal-members-from-outer-list%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              'app-layout' is not a known element: how to share Component with different Modules

                              android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                              WPF add header to Image with URL pettitions [duplicate]