Is there a way to declare temporary variables in expressions for reusing intermediary results?
The motivation is to still be able to use fluent style call chains and expression bodied methods when you need to use a calculated result more than once in logic or transformations e.g.
public static string Foo(IBar bar)
{
var result = bar.LongCalculation();
return result.HasError
? throw new Exception()
: result.ToString();
}
I thought I might be able to use let/select linq query keywords but it doesn't seem possible without a from clause e.g.
public static string Foo(IBar bar) =>
let result = bar.LongCalculation()
select result.HasError
? new Exception()
: result.ToString();