我需要通过发送列表进行api调用
List<string> fieldDurations = new List<string>
{"4h", "12h", "16h", "1d", "2d", "3d", "4d", "5d", "6d", "7d"};
我不想同时发送全部列表或一一发送,我需要增加它(旧值+新值)。 像这样:
Call 1: var test =feasiApiV1TestFeasiPostWithHttpInfo(fieldDurations) should have "4h"
call 2: var test =feasiApiV1TestFeasiPostWithHttpInfo(fieldDurations) should have "4h", "12h"
call 3: var test =feasiApiV1TestFeasiPostWithHttpInfo(fieldDurations) should have "4h", "12h", "16h"
,依此类推。有什么想法我可以做到吗?
答案 0 :(得分:3)
您可以在for
循环中遍历列表,并始终使用LINQ来获取所需的元素数量,如下所示:
for (int i = 1; i <= fieldDurations.Count; i++)
{
fieldDurations.Take(i);
}
这会遍历数组的Count,并从第一个元素开始获取i
个元素。 i
从1
开始而不是0
,因为Take(0)
没有用
编辑:感谢注释中的Kyle Polansky,指出循环条件应为i <= fieldDurations.Count