Home > Software engineering >  Get the four newest date in an Array and ath them to new Array
Get the four newest date in an Array and ath them to new Array

Time:09-23

I was wondering who to add the four latest NewsEntries to an array

IEnumerable<PageNewsEntry> allNews = node.Children<PageNewsEntry>();
IEnumerable<PageNewsEntry> latestNews = new List<PageNewsEntry>();

First array, has all the news on it. Second array needs to get "foreached" in order to render the html.

A PageNewsEntry has the property Date on it which is da Date. I was wondering how to add the four latest news to the latestNews - Array

I get the date from a news like this: allnews.foreach(var news in allNews){news.Date}

Thanks for your help!

CodePudding user response:

how to add the four latest NewsEntries to an array

You can use OrderByDescending and Take

var top4LatestNews = allNews.OrderByDescending(n => n.Date).Take(4).ToList();

If you really need an array(in your code you use a list), use ToArray.

  • Related