I have a:
DateTime?[] dates
I need to convert to:
Double?[] oaDates
Any way to clean up this one liner? Or should I just resort to a function?
DateTime?[] dates = new DateTime?[]{DateTime.Now, DateTime.Now.AddDays(1), DateTime.Now.AddDays(2)}; //example array
Double?[] oaDates = dates.Select(t => (t==null)?((double?)null):(t??DateTime.MinValue).ToOADate()).ToArray();
I can’t call a method on t without checking that it’s null (hence the tertiary operator) and I have to coalesce it as well so that I can call OADate (hence the coalesce and filler DateTime.MinValue that will never be hit).
It gets the job done in one line, but it is looking a little perl-like.
Maybe an extension method? That way I can hide the ugliness and just do something along the lines of:
dates.ToNullableOADates();
You can actually call extension methods on
nullreferences. So you could define an extension method to convert a singleDateTime?to adouble?like:Then call it like:
However, you could equally call it using the method group syntax: