[CSHARP-2708] Verify scenarios involving string.IsNullOrEmpty with or without null coalescing in argument Created: 23/Aug/19  Updated: 28/Oct/23  Resolved: 13/Oct/21

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: None
Fix Version/s: 2.14.0

Type: Improvement Priority: Major - P3
Reporter: Richard Collette Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: rp-track
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

This works

Poject(p=> p.PropertyName??"") 

But in the where clause of a LINQ query

where string.IsNullOrEmpty(product.PropertyName??"")

I get the error

InvalidOperationException: ({document}{propertyName} ?? "") is not supported.
MongoDB.Driver.Linq.Translators.PredicateTranslator.GetFieldExpression(Expression expression)



 Comments   
Comment by Robert Stam [ 13/Oct/21 ]

This issue has been fixed in the new LINQ provider (known as LINQ3) which will be included in the upcoming 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

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

Comment by Githook User [ 13/Oct/21 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-2708: Verify that string.IsNullOrEmpty with or without null coalescing argument works in LINQ3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/ffab9c65c8ea62479f684bac3aeed2cbe5a998ff

Comment by Robert Stam [ 08/Oct/21 ]

Tested Aggregate using Match and Project.

Tested AsQueryable using Select and Where.

Note that some scenarios require using `$expr` in the `$match` filter because they can't be expressed using filter language alone.

Comment by Richard Collette [ 23/Aug/19 ]

Actually, it does not work in a .Match() method expression either. 

Generated at Wed Feb 07 21:43:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.