Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2674

string.IsNullOrEmpty is false when field does not exist.

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.14.0
    • Affects Version/s: None
    • Component/s: Linq
    • Labels:

      Driver version 2.8.2

      string.isNullOrEmpty utilizes a test for null equality using

                                 "$or":[
                                    {
                                       "$eq":[
                                          "$someField",
                                          null
                                       ]
                                    },
                                    {
                                       "$eq":[
                                          "$someField",
                                          ""
                                       ]
                                    }
                                 ]
                              }
      

      However, if the field is not present, the null equality operation returns false.

      The null coalescing operator (??) on the other hand does work with both nulls and non-existing fields, and uses the $ifNull operation rather than equality.

      "Url":{
                     "$ifNull":[
                        "$shopUrl",
                        "$typeUrl"
                     ]
                  },
      

      It seems that $ifNull would be a better operation to use in the composed string.IsNullOrEmpty() method.

      Without this, we have to resort to strange expressions like:

      isExternalUrl = string.IsNullOrEmpty((pt.ShopUrl ?? "")),
      

      If I don't use the null coalescing operator in that example, the evaluation will return false when the field does not exist.

            Assignee:
            Unassigned Unassigned
            Reporter:
            rcollette@yahoo.com Richard Collette
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: