I have an array of (always 4) objects, which I need to order by descending value of an object member.
I had thought to order it as
Array = Array.OrderByDescending(p => p.Val)
This fell over when one of the values was null, of course. So what I am aiming for, but my LINQ is not up to, is:
Array = Array.OrderByDescending(p => if( p != null ) p.Val; else float.MinValue)
How can I accomplish this ordering without having to delete and later re-add the null value? Thanks for your help.
Use the ternary conditional operator:
Per the comments below, the reason you can’t use the
if/elseis because the body of the lambda (the stuff to the right ofp =>) must be an expression, unless you surround the whole thing with curly braces. So to illustrate, you could also use theif/elseif you wanted:But clearly more verbose.