![](/img/trans.png)
[英]C# take x items from list where each one has a unique value for a certain property
[英]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
个元素。 i
从1
开始而不是0
因为Take(0)
没用
编辑:感谢Kyle Polansky在评论中指出循环条件应该是i <= fieldDurations.Count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.