-1

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

@foreach (var Article in _Articles.OrderByDescending where _article.CreatedOn )
 {}
Coder_
  • 19
  • 9

2 Answers2

4

Try this:

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

Make sure to import the System.Linq namespace.

JuanR
  • 7,405
  • 1
  • 19
  • 30
  • Thank you i know its very simple just couldn't work it out – Coder_ Oct 26 '22 at 14:47
  • 1
    @Coder175 that's OK. What matters is that you try and keep going. That's what the community is for. Don't forget to accept my answer if you found it useful! – JuanR Oct 26 '22 at 14:47
1

Try using Linq

@foreach(var Article in _Articles.Where(c => c.CreatedOn >= <<Date Here>>))
{}
Mauricio Atanache
  • 2,424
  • 1
  • 13
  • 18