C# 如何限制在 foreach 循环中迭代的元素数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2014364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I limit the number of elements iterated over in a foreach loop?
提问by test
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 个项目而不是所有项目,我如何在 C# 中做到这一点?
采纳答案by hackerhasid
just iterate over the top 6 from the collection:
只需遍历集合中的前 6 个:
foreach(var rssItem in rss.Channel.Items.Take(6))
回答by Wouter
rss.Channel.Items.Take(6)
回答by jason
Use Enumerable.Take
:
foreach(var rssItem in rss.Channel.Items.Take(6)) {
// go time!
}
Note that
注意
rss.Channel.Items.Take(6)
does not do anything except instantiate an implementation of IEnumerable
that can be iterated over to produce the first six items in the enumeration. This is the deferred execution feature of LINQ to Objects.
除了实例化一个IEnumerable
可以迭代以生成枚举中的前六个项目的实现之外,不做任何事情。这是 LINQ to Objects 的延迟执行功能。
Note further that this assumes .NET 3.5. If you are working with an earlier version of .NET, you could use something along the lines of the following:
进一步注意,这假定 .NET 3.5。如果您使用的是 .NET 的早期版本,则可以使用以下内容:
static IEnumerable<T> Take<T>(IEnumerable<T> source, int take) {
if (source == null) {
throw new ArgumentNullException("source");
}
if (take < 0) {
throw new ArgumentOutOfRangeException("take");
}
if (take == 0) {
yield break;
}
int count = 0;
foreach (T item in source) {
count++;
yield return item;
if (count >= take) {
yield break;
}
}
}
Then:
然后:
foreach(var rssItem in Take(rss.Channel.Items, 6)) {
// go time!
}
This assumes .NET 2.0. If you're not using .NET 2.0 you should seriously consider upgrading.
这假设 .NET 2.0。如果您不使用 .NET 2.0,您应该认真考虑升级。
回答by Nick
Not to be too obvious but...
不要太明显,但是...
int max = Math.Min(6, rss.Channel.Items.Count);
for (int i = 0; i < max; i++)
{
var rssItem = rss.Channel.Items[i];
//...
}
I know it's old school, and not filled with all sorts of Extension method goodness, but sometimes the old school still works... especially if you're still using .NET 2.0.
我知道这是老派,并没有充满各种扩展方法的优点,但有时老派仍然有效……尤其是如果您仍在使用 .NET 2.0。
回答by p.campbell
If you're interested in a condition (i.e. ordering by date created)
如果您对条件感兴趣(即按创建日期排序)
foreach(var rssItem in rss.Channel.Items.OrderByDescending(x=>x.CreateDate).Take(6))
{
//do something
}
Perhaps if you want to get those created by a certain user, with that same sort
也许如果你想得到某个用户创建的那些,使用相同的排序
foreach(var rssItem in rss.Channel.Items
.Where(x=>x.UserID == 1)
.OrderByDescending(x=>x.CreateDate)
.Take(6))
{
//do something
}
回答by mdm20
You could also just break out of the loop if you don't want to use linq.
如果您不想使用 linq,也可以跳出循环。
int count = 0;
foreach (var rssItem in rss.Channel.Items)
{
if (++count == 6) break;
...
}