C
C#16mo 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
ero16mo ago
OrderByDescending?
list.OrderByDescending(foo => foo.Num1 - foo.Num2);
list.OrderByDescending(foo => foo.Num1 - foo.Num2);
GameShark
GameShark16mo ago
Oh. Well I feel dumb. Thanks!
PatrickG
PatrickG16mo ago
wait do you want a return list of just the result of this substraction tho?
ero
ero16mo 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
Accord16mo 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.