This question maybe stupid and very basic, but I dont find a way to write it nicely, excepts with extension methods, and I'm not sure there is no way to do what I want.
I know the basic :
result = (a != 0 ? a : b);
But when a is a long operation or result of a query, this become
result = (aReallyLongOperationOrQueryWithIncludes != 0 ? aReallyLongOperationOrQueryWithIncludes : b);
but I dont want to execute twice the aReallyLongOp... OK, you see :)
This is exactly what a coalesce operator like ?? does (predicate is == null), but I cant find one for a custom predicate, excepts by assigning a new variable but I wonder if I can skip this line :
var v = a;
result = (v != 0 ? v : b);
In some cases I use Math.Max(a,b) **, but it only works when the fallback cant be negative and has to be applied on zero or negative values...
Thanks for reading.
** the main usage of this is because EF Core dont returns null when a query return no rows, and the DefaultIfEmpty int returns 0, without ( known by me ) way to ask EF to return null.
An example of aReallyLongOperationOrQueryWithIncludes that needs to return zero if the balance is negative ( sometimes only till a certain amount ) :
return context.Realisations
.Include(x => x.Artist)
.Where(x => x.Artist == Artist)
.Where(x => x.Delivered == true)
.Sum(x => (x.CreationCost * x.Releases * x.Artist.PM) + x.Artist.UniqueFee - x.Artist.MonthlyCharges);