添加值到列表 [英] Adding values to List
问题描述
下面是我的代码,
List<float?> LValues= new List<float?>();
List<float?> IValues= new List<float?>();
List<float?> BValues= new List<float?>();
List<object> data = new List<object>();
float? Value_Likelihood_fromList = 0;
float? Value_Impact_fromList = 0;
float? Value_Bubblesize_fromList = 0;
foreach (var item in Read_xml_for_childobjects_id.Root.Descendants("object"))
{
for (int k = 0; k < 7; k++)
{
Value_LValues_fromList = LValues.ElementAt(k);
Value_IValues_fromList = IValues.ElementAt(k);
Value_BValues_fromList = BValues.ElementAt(k);
data.Add(new Data { x = Value_LValues_fromList, y = Value_IValues_fromList, z = Value_BValues_fromList });
// data.Add(data_content);
}
}
在这里,我的列表包含在每个28值喜欢
Here, my list contains 28 values in each like
LValues=[a1,a2,a3,a4....a28],
IValues=[b1,b2,b3...b28],
BValues=[c1,c2,c3...c28]
和我的数据列表越来越形式,
and my data list is getting in the form,
data=[[a1,b1,c2],[a2,b2,c2],[a3,b3,c3].....[a28,b28,c28]]
但我希望得到四个不同的数据列表如下:
but I want to get four different data list as follows
data=[[a1,b1,c1],[a2,b2,c2]....[a7,b7,c7]],
data=[[a8,b8,c8],[a9,b9,c9]....[a14,b14,c14]],
data=[[a15,b15,c15],[a16,b16,c16]....[a21,b21,c21]],
data=[[a22,b22,c22],[a23,b23,c23]....[a28,b28,c28]],
我知道它非常愚蠢的我问这个逻辑问题,但我现在面临真正的麻烦要做到这一点,任何帮助将不胜感激。
I know its very stupid of me to ask this logic question but I am facing real trouble to do this,any help will be greatly appreciated..
请注意,我通过阅读XML字符串填充列表
PLease note,I am populating the list by reading the xml string
有关参考下面是我如何得到列表值,这是唯一一个名单,我做对于另外两个同样的方式为好,
for reference below is how I get values in list,this is for only one list,I do in same manner for other two as well,
for (int j = 0; j < value_BeforeOffset_l.Count; j++)
{
var xmlAttributeCollection_for_period_BeforeOffset_L = value_BeforeOffset_l[j].Attributes;
if (xmlAttributeCollection_for_period_BeforeOffset_L != null)
{
if (i == 0 && s == 0)
{
var periodid = xmlAttributeCollection_for_period_BeforeOffset_L["periodid"];
xmlActions_Value_BeforeOffset_L[j] = periodid.Value;
period_final_id = periodid.Value;
try
{
period_name = ServiceClient.GetAttributeAsString(sessionId, periodid.Value, "name", "");
if (period_Name.Count() <= 7)
{
period_Name.Add(period_name);
}
}
catch (Exception ex)
{
ErrorValue = "Error found...Kindly Check Logs";
logger.ErrorException("QPR Web Service ERROR", ex);
return ErrorValue;
}
}
if (s == 0)
{
try
{
var action = xmlAttributeCollection_for_period_BeforeOffset_L["value"];
period_final_value = float.Parse(action.Value);
LValues.Add(period_final_value);
}
catch (Exception ex1)
{
LValues.Add(null);
}
}
}
}
- -------更新问题-----
--------Updated Question-----
foreach (var item in Read_xml_for_childobjects_id.Root.Descendants("object"))
{
for (int k = 0; k < 7; k++)
{
Value_LValues_fromList = LValues.ElementAt(k);
Value_IValues_fromList = IValues.ElementAt(k);
Value_BValues_fromList = BValues.ElementAt(k);
data.Add(new Data { x = Value_LValues_fromList, y = Value_IValues_fromList, z = Value_BValues_fromList });
// data.Add(data_content);
}
}
有在Read_xml_for_childobjects_id 4项,所以如果我需要要利用以下语句,
There are 4 items in Read_xml_for_childobjects_id, so if I need to make use of Following stmt,
data[i] = new List<Data>(); // first initialize this List instance
据亨克holtermen回答那该怎么做..? ?
According to Henk holtermen answer then how to do that..???
推荐答案
最基本的做法:
//List<object> data = new List<object>();
List<Data>[] data = new List<Data>[4]; // why use <object> here ?
//...
for(int i=0;i<4;i++)
{
data[i] = new List<Data>(); // first initialize this List instance
for (int k = 0; k < 7; k++)
//... your current code
}
这篇关于添加值到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!