2012-02-20 256 views
2

enter image description here如何动态检查数组中的索引是否存在?

我已经反序列化JSON字符串到CURRENT_LOCATION的阵列,EducationHistory和WorkHistory 我我遇到的问题是,不同的人可能有教育史上的不同规格,因此增加或减少阵列中的索引数

如何动态地解决这个问题,该值存储在数据库中

编辑:

Result soap = serializer.deserialize<Result>(ser); 
     foreach(var data in soap.data) 
     { 
     int lenght= soap.data.educationhistory.Length; 
     foreach(var education in soap.data.educationhistory) 
      { 
       // my insert query 
      } 
     } 

现在的问题是我将不得不为work_history运行一个foreach循环。在foreach循环中的两个foreach循环是一个问题。 如何最小限度地使用循环?

+0

为什么两个的foreach的问题吗? – 2012-02-20 10:26:11

+0

这不是我必须放的唯一的foreach,因为我想插入workhistory以及当前位置以及 – 2012-02-20 12:54:55

回答

1

您可以检查数组的长度:

int length = soap.data.education_history.Length; // gives you 2 

一旦你知道你知道,指标必须大于这个长度小的长度。当然,如果你只是遍历这个数组,你不会有任何索引的问题:

foreach (var education in soap.data.education_history) 
{ 
    // TODO: save the education in your database 
} 
+0

检查编辑! – 2012-02-20 09:07:26

相关问题