![](/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.