简体   繁体   English

如果在C#中Value不为null,则将“ Value”设置为对象“ Only”中的字段

[英]Setting a Value to a field inside an object “Only” if the Value is not null in C#

I am new to C#. 我是C#的新手。 I can use your help with the below. 我可以在下面使用您的帮助。 I have the following code. 我有以下代码。

    private void foo(TropicalRequest tropicalRequest)
    {
    var buildRequest = new RestRequest()
     {
      BaseUrl = tropicalRequest.baseUrl,
      StatusCode = tropicalRequest.statusCode,
      InitialDate = tropicalRequest.createdDate.Value
     };
    //Code call to save into DB
    }

The "tropicalRequest.createdDate.Value" field does not contain value for every scenario, when it is null my code breaks. “ tropicalRequest.createdDate.Value”字段不包含每个方案的值,如果该字段为null,则代码中断。 I have written the below code but I want to optimize it, your help is much appreciated. 我已经编写了以下代码,但是我想对其进行优化,非常感谢您的帮助。

    private void foo(TropicalRequest tropicalRequest)
    {
    var buildRequest = new RestRequest()
     {
      BaseUrl = tropicalRequest.baseUrl,
      StatusCode = tropicalRequest.statusCode
     };

    if(tropicalRequest.createdDate.HasValue)
      buildRequest.InitialDate = tropicalRequest.CreatedDate.Value;
    //Code call to save into DB
    }

Basically, I want to set the value to the field inside the object only if the value is not null. 基本上,我想仅在值不为null时才将值设置为对象内的字段。

Edit #1: InitialDate and CreatedDate are both of the DataType DateTimeOffset. 编辑1:InitialDate和CreatedDate都是DataType DateTimeOffset。

Edit #2: InitialDate is not nullable and CreatedDate is nullable DateTimeOffset. 编辑#2:InitialDate不能为空,而CreatedDate可以为null的DateTimeOffset。

You can use null propogation operator: 您可以使用null传播运算符:

var buildRequest = new RestRequest()
 {
  BaseUrl = tropicalRequest.baseUrl,
  StatusCode = tropicalRequest.statusCode,
  InitialDate = tropicalRequest.createdDate ?? default(DateTimeOffset)
 };

An important note, the Default value for the DateTime object is its MinValue which means if you have not assign anything means its value will be 01/01/0001 00:00:00 . 重要说明,DateTime对象的Default值是其MinValue ,这意味着如果您未分配任何内容,则其值将为01/01/0001 00:00:00 Consider tropicalRequest.CreatedDate is null in the second example then buildRequest.InitialDate will be the minimum value. 在第二个示例中,考虑tropicalRequest.CreatedDatenull ,则buildRequest.InitialDate将是最小值。

If you want it to be null means it should be of Nullable type. 如果您希望它为null意味着它应该为Nullable类型。 you can use the following syntax for defining them. 您可以使用以下语法来定义它们。

 DateTime? InitialDate; 

If you declared like this in the class, then you need not to check with HasValue property. 如果在类中这样声明,则无需检查HasValue属性。 you can directly assign like this InitialDate = tropicalRequest.createdDate 您可以像这样直接分配InitialDate = tropicalRequest.createdDate

As the other answer stated, you can try Null-conditional Operators operator If you are using c# 6.0 . 如其他答案所述,如果您使用的是c# 6.0 ,则可以尝试Null-conditional Operators运算符。 or else Make use of conditional operator : 否则使用条件运算符:

var buildRequest = new RestRequest()
 {
  BaseUrl = tropicalRequest.baseUrl,
  StatusCode = tropicalRequest.statusCode,
  InitialDate = tropicalRequest.createdDate.HasValue ? tropicalRequest.CreatedDate.Value : DateTime.MinValue;
 };

It will already be optimized by the compiler. 编译器已经对其进行了优化。 If you just want to format the code differently you can use this syntax. 如果只想以不同的方式设置代码格式,则可以使用此语法。 So the CreatedDate will be used if there is a value and otherwise the value provided on the other side of the ?? 因此,如果有值,将使用CreatedDate,否则在??的另一侧提供该值。 syntax, in this case default(DateTimeOffset) which will be the same as not assigning. 语法,在这种情况下为default(DateTimeOffset),它将与未分配的相同。 So one could argue your current syntax is actually "better" than this. 因此,有人可能会说您当前的语法实际上比这更好。

private void foo(TropicalRequest tropicalRequest)
{
    var buildRequest = new RestRequest()
    {
        BaseUrl = tropicalRequest.baseUrl,
        StatusCode = tropicalRequest.statusCode,
        InitialDate = tropicalRequest.CreatedDate ?? default(DateTimeOffset);
    };
}

As your InitialDate is non null DateTimeOffset it will be always initialized to Default value of the DateTimeOffset even if you do not assign it. 由于您的InitialDate非null,所以DateTimeOffset始终会初始化为DateTimeOffset的默认值,即使您未分配它也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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