tags:

views:

157

answers:

3

Hey, I want to make this return the result, but in descending order. Is that possible?

 var newList = list.OrderBy(x => x.Product.Name).toList();

I thought I could just add descending, like I do in other cases, but it is not accepting that.

+16  A: 

Sure:

var newList = list.OrderByDescending(x => x.Product.Name).ToList();

In response to your comment:

var newList = list.OrderByDescending(x => x.Product.Name)
                  .ThenBy(x => x.Product.Price)
                  .ToList();
StriplingWarrior
@stripling Thanks! Would it be possible to do some sort of orderbydescending, but add a thenby where the thenby was ascending?
PFranchise
Yes, it's easy. See my updated answer.
StriplingWarrior
So your edit will sort by name(from z->a) then price (low -> high)?
PFranchise
Yes, that is correct. Calls to OrderBy or ThenBy are always ascending. The OrderByDescending and ThenByDescending methods are what you'd use for descending.
StriplingWarrior
@stripling Thank you sir. Have a great day.
PFranchise
+4  A: 

Yes. Use OrderByDescending instead of OrderBy.

Mark Byers
@Downvoter: What is the problem with this answer? Could you please explain your reason?
Mark Byers
Because I **HATE** unicorns!
jjnguy
I hope my sarcasm was obvious...But, if it wasn't, I didn't downvote you.
jjnguy
LOL. Whoever it was must hate funny diamond things, because they downvoted my answer as well.
StriplingWarrior
Evidently they saw the error in their ways. The downvotes have been removed.
StriplingWarrior
+2  A: 
list.OrderByDescending();

works for me.

Shahin