[CSHARP-2674] string.IsNullOrEmpty is false when field does not exist. Created: 01/Aug/19 Updated: 28/Oct/23 Resolved: 15/Feb/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Linq |
| Affects Version/s: | None |
| Fix Version/s: | 2.14.0 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Richard Collette | Assignee: | Unassigned |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | rp-track | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Epic Link: | CSHARP-3615 | ||||||||
| Description |
|
Driver version 2.8.2 string.isNullOrEmpty utilizes a test for null equality using
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.
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:
If I don't use the null coalescing operator in that example, the evaluation will return false when the field does not exist. |
| Comments |
| Comment by James Kovacs [ 15/Feb/22 ] | ||||||||||||||||
|
This issue has been fixed in the new LINQ provider (known as LINQ3), which is included in the 2.14 release. Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality. To configure a client to use the LINQ3 provider use code like the following
| ||||||||||||||||
| Comment by Richard Collette [ 23/Aug/19 ] | ||||||||||||||||
|
While the null coalescing operator (??) will work in a .Project() lambda expression, it does not work in a LINQ query (which seems odd). This makes this issue a blocker for LINQ queries. So this does not work for me.
|