Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add a condition to a Foreach loop c#

Hi I have a foreach loop which lists out all the articles in a service layer, all the articles have a public system namespace DateTime attached to them. I want to edit the foreach loop so i can add a condition to sort the articles descending using the time they where created.

This is what I have at the moment below

@foreach (var Article in _Articles)
 {}

This is what I want but obviously this doesn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@foreach (var Article in _Articles.OrderByDescending where _article.CreatedOn )
 {}

>Solution :

Try this:

@foreach (var Article in _Articles.OrderByDescending(a => a.CreatedOn))
{
    //Your code here.
}

Make sure to import the System.Linq namespace.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading