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

时间:2021-07-01 07:15:55

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

I have deserialized a JSON string into arrays of current_location, EducationHistory and WorkHistory The problem i am encountering is that different people might have different specifications of education history and therefore increasing or decreasing the number of indexes in the array

我将JSON字符串反序列化为current_location、EducationHistory和WorkHistory的数组,我遇到的问题是不同的人可能有不同的教育历史规范,因此增加或减少数组中的索引数量。

How do i resolve dynamically this problem and store the values in the database

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

Edit:

编辑:

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

Now the problem is I will have to run a foreach loop for work_history also. Two foreach loop inside a foreach loop is an issue. How can it be done in minimum use of loops??

现在的问题是,我还必须为work_history运行一个foreach循环。foreach循环中的两个foreach循环是一个问题。如何在最少使用循环的情况下做到这一点?

1 个解决方案

#1


1  

You could check the length of the array:

你可以检查数组的长度:

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

Once you know the length you know that indexes must be smaller than this length. Of course if you simply loop through this array you won't have any problems with indexes:

一旦你知道了长度,你就知道指数一定小于这个长度。当然,如果只是循环遍历这个数组,那么索引就不会有任何问题:

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

#1


1  

You could check the length of the array:

你可以检查数组的长度:

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

Once you know the length you know that indexes must be smaller than this length. Of course if you simply loop through this array you won't have any problems with indexes:

一旦你知道了长度,你就知道指数一定小于这个长度。当然,如果只是循环遍历这个数组,那么索引就不会有任何问题:

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