繁体   English   中英

从列表中逐个获取项目而不会丢失旧值

[英]Take items from list one by one without losing the old value

我需要通过发送一个列表来进行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"

等等。 任何想法我怎么能这样做?

您可以在for循环中遍历List并始终使用LINQ来获取所需的元素数量,如下所示:

for (int i = 1; i <= fieldDurations.Count; i++)
{
    fieldDurations.Take(i);
}

这将迭代数组的Count,并从第一个开始获取i个元素。 i1开始而不是0因为Take(0)没用

编辑:感谢Kyle Polansky在评论中指出循环条件应该是i <= fieldDurations.Count

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM