doglin
doglin

Reputation: 1741

what is the right syntax for OrderByDescending with condition?

I would like to know what is the correct syntax for OrderByDescending with condition

I have the following code,

_mobileRepository.GetAllEpisodes().OrderByDescending(x => x.AirDate).ToList();

But now, i want to only list AirDate > = 2012, so Could I do the following?

_mobileRepository.GetAllEpisodes().OrderByDescending(x => (x.AirDate> 2012)).ToList();

what is the correct syntax here?

Upvotes: 0

Views: 123

Answers (4)

alexm
alexm

Reputation: 6882

To filter data you can use the Where extension method:

mobileRepository.GetAllEpisodes()
   .Where(x=>x.AirDate > 2012)
   .OrderByDescending(x => x.AirDate).ToList();

Upvotes: 1

I4V
I4V

Reputation: 35353

First filter for AirDate > = 2012 then apply OrderByDescending

_mobileRepository.GetAllEpisodes().Where(x =>  x.AirDate >= 2012)
                               .OrderByDescending(x => x.AirDate).ToList();

Upvotes: 2

Guffa
Guffa

Reputation: 700302

To filter the result, you use Where not OrderByDescending:

_mobileRepository.GetAllEpisodes()
  .Where(x => x.AirDate >= 2012)
  .OrderByDescending(x => x.AirDate)
  .ToList();

Upvotes: 2

Scott Selby
Scott Selby

Reputation: 9570

_mobileRepository.GetAllEpisodes().Where(x => x.AirDate >= 2012).OrderByDescending(x => x.AirDate).ToList();

Upvotes: 2

Related Questions