I’ve got a foreach loop fetches everything, but I want it to stop after 10. It appears that I should use for instead. But I don’t understand how to write that while still assigning the required var.
@foreach (var p in posts)
{
<item>
<title>@p.GetProperty("zContentTitle").Value</title>
<dc:creator>@p.GetProperty("zPostAuthor").Value</dc:creator>
<category>@p.GetProperty("zPostCategories")</category>
<description>@p.GetProperty("zContentBody").Value.StripHtml().Trim()</description>
<link>http://@Request.Url.Host@landing.Url@p.Url</link>
<guid isPermaLink="false">http://@Request.Url.Host@landing.Url@p.Url</guid>
<pubDate>@p.GetProperty("zPostDate").Value.FormatDateTime("ddd, dd MMM yyyy HH:mm:ss") CST</pubDate>
</item>
}
Given that small amount of code it’s hard to tell, which of the many options you should use. It seems that the “posts” in
is of type IEnumerable. So you could use the extension method Take(). Your code would look like this: