How could I take 1 more item from Linq's TakeWhile?

后端 未结 5 2034
难免孤独
难免孤独 2021-01-01 16:45

(line of code of interest is the last one, the rest is just for a full representation)

Using the following code, I wanted to take VOTERS until I exceeded

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-01 17:24

    Variation of Kobi's answer but demonstrates use of (value, index). index is useful in solving similar issues albeit not OP's.

    voters.TakeWhile((value, index) => (voicesSoFar += value.Voices) - value.Voices < voicesNeeded);
    

提交回复
热议问题