How do I limit the number of elements iterated over in a foreach loop?

前端 未结 6 2112
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 08:34

I have the following code

foreach (var rssItem in rss.Channel.Items)
{
    // ...
}

But only want 6 items not all items, how can I do it in C#?

6条回答
  •  [愿得一人]
    2021-02-05 09:02

    just iterate over the top 6 from the collection:

    foreach(var rssItem in rss.Channel.Items.Take(6))
    

提交回复
热议问题