简体   繁体   中英

How to check null or datetime value using ternary operator?

I want to check passed value is null or datetime value using ternary operator in c#?

I tried like this

fromDate == null ? null : Convert.ToDateTime(fromDate)

getting error:

type of conditional expression cannot be determined

I want to check whether variable fromDate is null or having date time value ?

variable fromDate is coming from Querystring and type of string.

From ?: Operator :

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

condition ? first_expression : second_expression;

Convert.ToDateTime returns DateTime and there is no implicit conversion between null and DateTime . And the conditional operator is an expression and that needs a return type.

One option seems logical to me using DateTime.TryParse (which returns boolean ) as a first expression and use another boolean ( true or false ) as a second expression . Damiths' answer seems logical.

Or instead you can use nullable DateTime like DateTime?

DateTime? foo;

if(foo.HasValue)
{
   //Nullable DateTime has a value..
}

if you have string value for fromDate do as below

DateTime dt;

bool isValidDate =  String.IsNullOrEmpty(fromDate) ? false : DateTime.TryParse(fromDate, out dt); 

if you know the datetime format/formats which your input having you better use DateTime.TryParseExact method

if fromDate is DateTime, then simple you can check as below

fromDate.HasValue

you don't need ?: Operator

The problem is that Convert.ToDateTime(fromDate) is of type DateTime, which cannot accept a null value, that is why this code won't work in this form. You have two choices to make. Either change it to regular if:

if(fromDate != null)
{
        Convert.ToDateTime(fromDate)
}

Or cast DateTime to nullable DateTime:

    fromDate == null ? null : (DateTime?)Convert.ToDateTime(fromDate)

Ternary operator in C# needs both values to be of the same type and it is explained here .

romDate == null ? null : Convert.ToDateTime(fromDate)

null and Convert.ToDateTime(fromDate) haven't common type. Compiler must be able to cast both expressions in canditional operator to same type.

您必须使用 String.IsNullOrEmpty 来检查 fromDate 是否为空,如下所示:

DateTime? date = String.IsNullOrEmpty(fromDte) ? null : (DateTime?)Convert.ToDateTime(fromDate)

I hope it will help you

   string format = "ddd dd MMM h:mm tt yyyy";
    DateTime dateTime;

    fromDate=(DateTime.TryParseExact(fromDate, format, CultureInfo.InvariantCulture,
        DateTimeStyles.None, out dateTime))?dateTime:null;

It looks like the main problem is that you are trying to assign null to a DateTime

DateTime is a structure and not a reference type so this can't be done.

Either use a nullable DateTime ( DateTime? ) or a specific value to indicate null, such as DateTime.MinValue

Have a look here for an example: http://www.dotnetperls.com/nullable-datetime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM