To get the data into list
我有两个清单如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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++; } |
现在我想输出如下,
1 2 3 4 | 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}]. |
号
现在,如果在for循环中为k,如果我把条件取为
1 2 3 4 | 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}], |
因为每次循环运行时它只遇到列表的前7个元素
如果我把循环作为
1 | Index was outside of bounds of the array.. |
。
任何帮助都将非常感谢,我知道我已经对同一个问题提出了两个以上的问题,但我怀疑我不能正确地解释我的问题,我希望这个问题是清楚的,
-------更新的问题-------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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); |
所以我得到的输出如下,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | {"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} ] } ] } |
。
现在我想要的输出如下,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | {"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} ] } ] }.. |
。
我必须把我要转换成JSON格式的数据转换成绘图图,而且数据标签是强制性的…我希望我能解释一下…
-------更新的问题-------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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); |
这是我的输出
1 2 3 4 5 6 7 8 9 10 | {"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}]]}]} |
。
--------更新了所需的输出-------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | {"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}]]}]} |
我希望数据字与JSON输出中的每个数据列表一起提供…哪个不来了……
您的代码只是PSUDO代码,无法工作。不过,我认为这正是您要做的,并提供了一个工作示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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); } } |
输出为:。
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————--请注意,您的代码需要具有:
1 2 3 4 5 |
号
正如我在评论中所说的
这里有一个基于LINQ操作符而不是显式迭代的解决方案。它使用来自Morelinq项目的批处理操作符-它在Nuget上可用。
将L、I和B值序列压缩在一起的行有点难看,但是.NET没有内置的3参数序列zip(zip是在.NET 4.0中添加的,早期版本的morelinq中有另一个实现)。我可能会实现自己的扩展方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using MoreLinq; namespace ConsoleApplication1 { class Program { public class HMData // Dummy HMDta class for test purposes { public string x { get; set; } public string y { get; set; } public string z { get; set; } } static void Main(string[] args) { var inputItems = new List<int>() { 1, 2, 3, 4 }; // Input items from XML document - dummy test list // L, I and B value sequences var LValues = new List<string>() {"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" }; var IValues = new List<string>() {"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" }; var BValues = new List<string>() {"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" }; // Convert our 3 -value sequences into one sequence of HMData objects var zippedValues = LValues.Zip(IValues, (lvalue, bvalue) => Tuple.Create(lvalue, bvalue)).Zip(BValues, (pair, bvalue) => new HMData() { x = pair.Item1, y = pair.Item2, z = bvalue }); // Split the sequence into length of length 7 var batchedValues = zippedValues.Batch(7); // For each of the inputItems, output a batch from the -value sequences // (note we will stop output as soon as either the inputItems or the combined -value sequences are exhausted) var data = inputItems.Zip(batchedValues, (item, valueBatch) => valueBatch).ToArray(); } } } |