C
C#2y ago
GameShark

✅ best LINQ method to sort after subtracting two properties?

I have a list of objects with two fields, num1 and num2. I would like to subtract num1 by num2 and then sort descending by the resulting value. Can anyone recommend a LINQ method for this?
5 Replies
ero
ero2y ago
OrderByDescending?
list.OrderByDescending(foo => foo.Num1 - foo.Num2);
list.OrderByDescending(foo => foo.Num1 - foo.Num2);
GameShark
GameSharkOP2y ago
Oh. Well I feel dumb. Thanks!
PatrickG
PatrickG2y ago
wait do you want a return list of just the result of this substraction tho?
ero
ero2y ago
ah yeah, maybe i misread the question that'd be
list.Select(foo => foo.Num1 - foo.Num2).Order();
list.Select(foo => foo.Num1 - foo.Num2).Order();
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server