1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need Some Help With Nested List Objects - C#

Discussion in 'C, C++, C#' started by Cnotey, Nov 29, 2015.

Tags:
  1. Cnotey

    Cnotey Power Member

    Joined:
    Jun 25, 2010
    Messages:
    713
    Likes Received:
    915
    Location:
    Seattle
    Home Page:
    Hey guys,

    I've been searching Stackoverflow for the answer I need, but I can't seem to find it.

    I have (3) different class objects, each level is a deeper list of a list.

    I can't seem to figure out how to get; and set; variables for list items more than one deep.

    Here's my code:

    Code:
            public void AddSecurities()        {
                for(int i = 0; i < symbolsList.Count; i++)
                {
                    AddSecurity(SecurityType.Equity, symbolsList.ElementAt(i).ToString(), Resolution.Minute);
                }
            }
            public void AddSymbols_toList()
            {
                for(int i = 0; i < SymbolsForAlgorithm.Length; i++)
                {
                    symbolsList.Add(new SymbolList { SymbolName = SymbolsForAlgorithm[i]});
                }
            }
            public class SymbolList
            {
                public string SymbolName { get; set; }
                public List<SymbolData> symbolData { get; set; } = new List<SymbolData>();
    
    
                public override string ToString()
                {
                    return String.Format("{0}", SymbolName);
                }
            }
            public class SymbolData
            {
                public List<SharePrices> completeData { get; set; } = new List<SharePrices>();
                public List<SharePrices> windowData { get; set; } = new List<SharePrices>();
            }
            public class SharePrices
            {
                public DateTime Date { get; set; }
                public decimal Price { get; set; }
    
    
                public override string ToString()
                {
                    return String.Format("{0}", "{1}", Date, Price);
                }
            }
    As you can see, I have no problem setting SymbolNames for my first list under my AddSecurities() class.

    What I need to do, is for each SymbolName in SymbolList add a class of type completeData to each symbolData for the corresponding SymbolName.

    Thanks in advance.
     
  2. neutralhatter

    neutralhatter Jr. VIP Jr. VIP

    Joined:
    Jun 23, 2010
    Messages:
    437
    Likes Received:
    332
    hi,

    I was typing an answer to your question but from your partial code snippet I would only be assuming certain things. Therefor let me comment on your first difficulty => namely = "I can't seem to figure out how to get; and set; variables for list items more than one deep."

    When trying to reach deeper listitems you can just use the following syntax:

    Code:
    int CityNumber = 1;
    int StreetNumber = 2;
    
    Cities[CityNumber].Streets[StreetNumber].StreetName = "BHWBoulevard";
    
    
    now the third street of the second City is called BHWBoulevard. Streets are a nested list inside of Cities.


    I would also like to point out that you have an object called symbolsList and a class called SymbolList in your code. I'm not sure that you are aware of this? This may be the cause of your confusion since the class SymbolList is not a list at all, it's just a class. Is symbolsList a list of SymbolList objects? if that is the case you would reach symboldata in the following fasion:

    Code:
    foreach(SymbolData sd in symbolsList[i].symbolData)
    {
    
    // execute logic for comparison => corresponding symbolname
    }
    
    Hope this helps,
     
    • Thanks Thanks x 1
  3. Cnotey

    Cnotey Power Member

    Joined:
    Jun 25, 2010
    Messages:
    713
    Likes Received:
    915
    Location:
    Seattle
    Home Page:
    Hey neutral thanks for the help. One thing I might not still be understanding. Don't I need to instance a new List<TYPE> for each class before I can store, and grab data from it?
     
  4. neutralhatter

    neutralhatter Jr. VIP Jr. VIP

    Joined:
    Jun 23, 2010
    Messages:
    437
    Likes Received:
    332
    Correct. Unless it's a static class. But since you want a list, the answer is yes. you should make a new instance.
     
    • Thanks Thanks x 1
  5. Cnotey

    Cnotey Power Member

    Joined:
    Jun 25, 2010
    Messages:
    713
    Likes Received:
    915
    Location:
    Seattle
    Home Page:
    Thanks. I think that's what I'm not getting.

    So Class SymbolList might have 20 items in the list, but class SymbolData might have 10,000 or 1,000,000 items in it. Do I need to create a new SymbolData list for each set of SharePrice data? How do I instance a new SymbolData and SharePrice data for each new Symbol in SymbolList?

    I was thinking it looks something like:

    Code:
    symbolsList.Add(new SymbolList { SymbolName = "blah",  symbolData = new SymbolData { completeData = new SharePrices { Date = 11/29/15, Price = 110.0} windowData = completeData -10 }});

    But this type of code keeps throwing an inheritance error.

    So I guess what I am still confused about is the new List<Type> and how to instance those and when.

    I should also add I HATE HATE working with LINQ. If it's the only way I understand, but I would prefer to not use LINQ.
     
    Last edited: Nov 29, 2015
  6. neutralhatter

    neutralhatter Jr. VIP Jr. VIP

    Joined:
    Jun 23, 2010
    Messages:
    437
    Likes Received:
    332
    as you probably know, linq is just a query language. linq is used if you need a specific item within a large list or a set of items within a large list.

    Since you want to touch all items in the list, you don't need linq and you also don't need to use the shorthand coding style that you are using.



    Since you are already initializing Symboldata inside SymolsList, there is no need to reinitialize.

    you can just do something like this.... (ps. I'm just typing by hand and I don't know the context of your code, so you may need to fix/modify what's wrong)
    Code:
    List<SymbolList> SymbolsList = new List<SymbolList>();
    foreach (SymbolList sl in SymbolsList)
    {
    SymbolData sd = newSymboldata();
    
    SharePrices sp = new SharePrices();
    sp.Price = 1;
    
    sd.Completedata.Add(sp);
    
      sl.Symboldata.Add(sd);
    
    }
    
    

     
    • Thanks Thanks x 1
  7. Cnotey

    Cnotey Power Member

    Joined:
    Jun 25, 2010
    Messages:
    713
    Likes Received:
    915
    Location:
    Seattle
    Home Page:
    Thank you so much. I think that's exactly what I need. I will try and implement tomorrow morning.