簡體   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