2012-02-20 95 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

相關問題