Reputation: 1741
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
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
Reputation: 35353
First filter for AirDate > = 2012
then apply OrderByDescending
_mobileRepository.GetAllEpisodes().Where(x => x.AirDate >= 2012)
.OrderByDescending(x => x.AirDate).ToList();
Upvotes: 2
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
Reputation: 9570
_mobileRepository.GetAllEpisodes().Where(x => x.AirDate >= 2012).OrderByDescending(x => x.AirDate).ToList();
Upvotes: 2