向List添加值

时间:2014-01-03 13:53:28

标签: c# 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个值,如

LValues=[a1,a2,a3,a4....a28],
IValues=[b1,b2,b3...b28],
BValues=[c1,c2,c3...c28]

我的数据列表正在填写

data=[[a1,b1,c2],[a2,b2,c2],[a3,b3,c3].....[a28,b28,c28]]

但我希望获得以下四种不同的数据列表

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]],

我知道问我这个逻辑问题是非常愚蠢的,但我真的很难做到这一点,任何帮助将不胜感激..

请注意,我通过阅读xml字符串

来填充列表

以下参考是我如何获得列表中的值,这只是一个列表,我也以同样的方式对其他两个列表,

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);
            }
        }
    }                         
}

--------更新问题-----

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项,所以如果我需要使用以下stmt,

  data[i] = new List<Data>();   // first initialize this List instance

根据Henk holtermen的回答然后该怎么做.. ???

1 个答案:

答案 0 :(得分:3)

最基本的方法:

//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
}