Question Multiple ternary question

beantownace

Active member
Joined
Feb 15, 2019
Messages
26
Programming Experience
5-10
Is this best way to set a nullable DateTime? for a object where I either have a order date of today or tomorrow then I set a date otherwise I need it to just be null.

code:
    var order = new OrderSetup
                {
                    Customer = Customer,
                    Total = Total,
                    OrderDate = string.Compare(OrderStatus, "Today", StringComparison.OrdinalIgnoreCase) == 0 ?
                                     Dates.TodayOrderDate :
                                     string.Compare(OrderStatus, "Tomorrow", StringComparison.OrdinalIgnoreCase) == 0 ?
                                     Dates.TommorrowOrderDate : (DateTime?)null //is this the best way where it seems like I have to set this as a nullable datetime.
                };
 
If you're using C# 8.0 or later then you can use a switch expression. If I'm not mistaken, that would look like this:
C#:
OrderDate = OrderStatus switch
{
        "Today" => Dates.TodayOrderDate,
        "Tomorrow" => Dates.TomorrowOrderDate,
        _ => (DateTime?) null
}
 
How do you force a case insensitive match with a switch?
 
How do you force a case insensitive match with a switch?
This seems to work, although doesn't look that elegant:
C#:
var OrderDate = OrderStatus switch
{
    string _ when OrderStatus.Equals("Today", StringComparison.CurrentCultureIgnoreCase) => Dates.TodayOrderDate,
    string _ when OrderStatus.Equals("Tomorrow", StringComparison.CurrentCultureIgnoreCase) => Dates.TomorrowOrderDate,
    _ => (DateTime?)null
};
 
How do you force a case insensitive match with a switch?
I kind of overlooked that part. Got all excited about using a switch expression, which I've never actually done before.
 
This seems to work, although doesn't look that elegant:
C#:
var OrderDate = OrderStatus switch
{
    string _ when OrderStatus.Equals("Today", StringComparison.CurrentCultureIgnoreCase) => Dates.TodayOrderDate,
    string _ when OrderStatus.Equals("Tomorrow", StringComparison.CurrentCultureIgnoreCase) => Dates.TomorrowOrderDate,
    _ => (DateTime?)null
};
I'd suggest that it's probably still more elegant than nested ternary operators though.
 
The only way, I can think of to make things cleaner like in post #2 is to use switch Order status?.ToUpper(), and then put all the literal strings in caps, but that assumes that the input language is not Turkish and that the literal string to be compared does not contain an I.
 
Last edited:
The only way, I can think of to make things cleaner like in post #2 is to use switch Order status?.ToUpper(), and then put all the literal strings in caps, but that assumes that the input language is not Turkish and that the literal string to be compared does not contain an I.
I can't help thinking that that OrderStatus should be an enum rather than a string and then case would be irrelevant. It depends what the possible values are but I would assume that only a specific set of values would be valid, in which case it absolutely should be an enum.
 
True. If the only choices are null, Today or Tomorrow, then a nullable date time seems to be overkill.
 
Back
Top Bottom