Search code examples
c#listfor-loopforeachxelement

To get the data into list


I have 2 list as below,

List<object>[] data = new List<object>[4];
List<HMData>[] Data_Content = new List<HMData>[7];
int indexer=0;

And also I have 3 list as LValues,IValues and BValues each containing 28 data values as follows

LValues={L1,L2,L3....L28},
IValues={I1,I2,I3...I28},
BValues={B1,B2,B3....B28},

foreach (var item in Read_xml_for_childobjects_id.Root.Descendants("object")) //  Contains 4 Items
  {
 for (int k = 0; k < 7; k++)
     {
       Data_Content[k] = new List<HMData>();
       Value_LfromList = LValues.ElementAt(k);
       Value_IfromList = IValues.ElementAt(k);
       Value_BfromList = BValues.ElementAt(k);

       Data_Content[k].Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
      }   
      data[indexer] = new List<object>(Data_Content);
      indexer++;
  }

now I want the output to be as follows,

data=[{L1,I1,B1},{L2,I2,B2},{L3,I3,B3},{L4,L4,B4},{L5,I5,B5},{L6,I6,B6},{L7,I7,B7}],
data=[{L8,I8,B8},{L9,I9,B9},{L10,I10,B10},{L11,I11,B11},{L12,I12,B12},{L13,I13,B13},{L14,I14,B14}],
data=[{L15,I15,B15},{L16,I16,B16},{L17,I17,B17},{L18,I18,B18},{L19,I19,B19},{L20,I20,B20},{L21,I21,B21}],
data=[{L22,I22,B22},{L23,I23,B23},{L24,I24,B24},{L25,I25,B25},{L26,I26,B26},{L27,I27,B27}{L28,I28,B28}].

Now if in for loop for k,if I take condition as for (int k = 0; k < 7; k++) then I get output as follows,

data=[{L1,I1,B1},{L2,I2,B2}.....{L7,I7,B7}],
data=[{L1,I1,B1},{L2,I2,B2}.....{L7,I7,B7}],
data=[{L1,I1,B1},{L2,I2,B2}.....{L7,I7,B7}],
data=[{L1,I1,B1},{L2,I2,B2}.....{L7,I7,B7}],

Since it encounters only first 7 elements of list each time the loop runs

And if I take for loop as for (int k = 0; k < 28; k++),since I have total 28 elements in each list, then after Data_Content list is filled with 7 elements since this is what I wanted and when 8 element is encountered then I get an error as

Index was outside of bounds of the array..

Any help will be greatly appreciated, I know I have placed two more questions on same problem but I doubt I was not able to explain my problem properly, I hope this question is clear,

-------updated question-------

 for (int k = 0; k < 4; k++)
  {
    for (int l = 0; l < 7; l++)
     { 
       Value_LfromList = LValues.ElementAt((k * 7) + l);
       Value_IfromList = IValues.ElementAt((k * 7) + l);
       Value_BfromList = BValues.ElementAt((k * 7) + l);
       Data_Content.Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
            }
        data.Add(Data_Content);
      }
   }

   var data_List = new { data = data };
   var series = new[] { data_List };
   var obj = new {chart,series };
   string result = jSearializer.Serialize(obj);

so the output I get is as follows,

{"chart":{"type":"bubble"},
 "series":
   [
      {"data":
         [
           {"x":7,"y":7,"z":49},{"x":7,"y":7,"z":49},{"x":7,"y":9,"z":63},
           {"x":5,"y":9,"z":45},{"x":4,"y":3,"z":12},{"x":2,"y":6,"z":12},
           {"x":3,"y":5,"z":15},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
           {"x":7,"y":8,"z":56},{"x":9,"y":6,"z":54},{"x":5,"y":7,"z":35},
           {"x":3,"y":8,"z":24},{"x":4,"y":3,"z":12},{"x":7,"y":8,"z":56},
           {"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":8,"y":7,"z":56},
           {"x":5,"y":7,"z":35},{"x":3,"y":7,"z":21},{"x":5,"y":8,"z":40},
           {"x":3,"y":7,"z":21},{"x":3,"y":7,"z":21},{"x":5,"y":2,"z":10},
           {"x":5,"y":2,"z":10},{"x":8,"y":6,"z":48},{"x":7,"y":3,"z":21},
           {"x":6,"y":7,"z":42}
         ]
      }
   ]
 }

now output I want is as follows,

{"chart":{"type":"bubble"},
 "series":
   [
      {"data":
         [
           {"x":7,"y":7,"z":49},{"x":7,"y":7,"z":49},{"x":7,"y":9,"z":63},
           {"x":5,"y":9,"z":45},{"x":4,"y":3,"z":12},{"x":2,"y":6,"z":12},
           {"x":3,"y":5,"z":15}],

        "data":
         [
           {"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
           {"x":9,"y":6,"z":54},{"x":5,"y":7,"z":35},{"x":3,"y":8,"z":24},
           {"x":4,"y":3,"z":12}],

        "data":
         [
           {"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
           {"x":8,"y":7,"z":56},{"x":5,"y":7,"z":35},{"x":3,"y":7,"z":21},
           {"x":5,"y":8,"z":40}],

         "data":
         [
           {"x":3,"y":7,"z":21},{"x":3,"y":7,"z":21},{"x":5,"y":2,"z":10},
           {"x":5,"y":2,"z":10},{"x":8,"y":6,"z":48},{"x":7,"y":3,"z":21},
           {"x":6,"y":7,"z":42}
         ]
      }
   ]
 }..

I have to convert data I am getting into JSON format to plot graph and also data tag is mandatory...I hope I explain you...

-------Updated question-------

for (int k = 0; k < 4; k++)
  {
    List<HMData> Data_Content = new List<HMData>();
    for (int l = 0; l < 7; l++)
     {
       Value_LfromList = LValues.ElementAt((k * 7) + l);
       Value_IfromList = IValues.ElementAt((k * 7) + l);
       Value_BfromList = BValues.ElementAt((k * 7) + l);
       Data_Content.Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
     }
     data_list.Add(Data_Content);
   } 
   var chart = new
    {
      type = ChartType
    };
   var data = new { data = data_list };
   var series = new[] { data };
   var obj = new {chart,series };
   string result = jSearializer.Serialize(obj);

and this is my output

{"chart":{"type":"bubble"},"series":[{"data":[[{"x":7,"y":7,"z":49},
{"x":7,"y":7,"z":49},{"x":7,"y":9,"z":63},{"x":5,"y":9,"z":45},{"x":4,"y":3,"z":12},
 {"x":2,"y":6,"z":12},{"x":3,"y":5,"z":15}],
[{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
 {"x":7,"y":8,"z":56},{"x":9,"y":6,"z":54},{"x":5,"y":7,"z":35},{"x":3,"y":8,"z":24},
 {"x":4,"y":3,"z":12}],
[{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
 {"x":8,"y":7,"z":56},{"x":5,"y":7,"z":35},{"x":3,"y":7,"z":21},{"x":5,"y":8,"z":40}],
[{"x":3,"y":7,"z":21},{"x":3,"y":7,"z":21},{"x":5,"y":2,"z":10},{"x":5,"y":2,"z":10},
 {"x":8,"y":6,"z":48},{"x":7,"y":3,"z":21},{"x":6,"y":7,"z":42}]]}]}

--------Updated Required output-------

{"chart":{"type":"bubble"},"series":[
{"data":[[{"x":7,"y":7,"z":49},
{"x":7,"y":7,"z":49},{"x":7,"y":9,"z":63},{"x":5,"y":9,"z":45},{"x":4,"y":3,"z":12},
 {"x":2,"y":6,"z":12},{"x":3,"y":5,"z":15}],

 "data":[{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
 {"x":7,"y":8,"z":56},{"x":9,"y":6,"z":54},{"x":5,"y":7,"z":35},{"x":3,"y":8,"z":24},
 {"x":4,"y":3,"z":12}],


  "data":[{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},{"x":7,"y":8,"z":56},
 {"x":8,"y":7,"z":56},{"x":5,"y":7,"z":35},{"x":3,"y":7,"z":21},{"x":5,"y":8,"z":40}],


  "data":[{"x":3,"y":7,"z":21},{"x":3,"y":7,"z":21},{"x":5,"y":2,"z":10},{"x":5,"y":2,"z":10},
 {"x":8,"y":6,"z":48},{"x":7,"y":3,"z":21},{"x":6,"y":7,"z":42}]]}]}

I want data word to come with every data list in json output... which is not coming....


Solution

  • Your code is only psudo code and will not work. However, I think this is what you are trying to do and provided a working example.

    internal class Program
    {
        private static void Main(string[] args)
        {
            List<List<HMData>> data = new List<List<HMData>>();
    
            string[] Ls =
            {
                "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9", "L10", "L11", "L12", "L13", "L14",
                "L15", "L16", "L17", "L18", "L19", "L20", "L21", "L22", "L23", "L24", "L25", "L26", "L27", "L28"
            };
            string[] Is =
            {
                "I1", "I2", "I3", "I4", "I5", "I6", "I7", "I8", "I9", "I10", "I11", "I12", "I13", "I14",
                "I15", "I16", "I17", "I18", "I19", "I20", "I21", "I22", "I23", "I24", "I25", "I26", "I27", "I28"
            };
            string[] Bs =
            {
                "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11", "B12", "B13", "B14",
                "B15", "B16", "B17", "B18", "B19", "B20", "B21", "B22", "B23", "B24", "B25", "B26", "B27", "B28"
            };
    
            for (int k = 0; k < 4; k++)
            {
                List<HMData> Data_Content = new List<HMData>();
                for (int j = 0; j < 7; j++)
                {
                    var l = Ls.ElementAt((k*7) + j);
                    var i = Is.ElementAt((k*7) + j);
                    var b = Bs.ElementAt((k*7) + j);
    
                    Data_Content.Add(new HMData {x = l, y = i, z = b});
                }
    
                data.Add(Data_Content);
            }
    
            foreach (var item in data)
            {
                Console.Write("data=[");
                for (int i = 0; i < 6; i++)
                {
                    Console.Write("{0},",item[i]);
                }
                Console.WriteLine("{0}]", item[6]);
    
            }
            Console.ReadLine();
    
        }
    }
    
    
    public class HMData
    {
        public string x;
        public string y;
        public string z;
    
        public override string ToString()
        {
            return string.Format("{{{0},{1},{2}}}", x, y, z);
        }
    }
    

    The output is: run example

    ------------------------------- EDIT FOR OP ---------------------------------- Note your code needs to have this:

    for (int k = 0; k < 4; k++)
    {
        List<HMData> Data_Content = new List<HMData>();
        ....
    }
    

    as i've said in the comments