[CSHARP-3213] Linq Translation of Any in Where Filter Throw NotSupportedException Created: 24/Sep/20  Updated: 27/Oct/23  Resolved: 29/Sep/20

Status: Closed
Project: C# Driver
Component/s: Builders
Affects Version/s: 2.7.2, 2.8.0, 2.9.0, 2.10.0, 2.11.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Oleksii Velchenko Assignee: Dmitry Lukyanov (Inactive)
Resolution: Works as Designed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

.Net Core 3.1
MongoDB Server 4.4.1, MongoDB Server 4.0.19, MongoDB Server 4.2.2



 Description   

 

I trying to find documents with filter below

 var filter = builder.Where(t => t.Array1.Any(l => !string.IsNullOrEmpty(model.ExternalId) && 
 l.ExternalId == model.ExternalId || 
 l.Name.ToLower() == model.Name.ToLower() ||
 t.Address.ToLower() == model.Address.ToLower()));

But in result got an error 

System.NotSupportedException: The LINQ expression: {document}{Locations}.Where((((False AndAlso ({document}{ExternalId} == null)) OrElse ({document}{Name}.ToLower() == "loc")) OrElse ({document}{Address}.ToLower() == "address"))) has the member "t" which can not be used to build a correct MongoDB query.

On Driver version 2.7.2, .NetCore 2.1 and MongoDB Server 4.2 it works fine.

It seems to be related to CSHARP-2705 but not sure how to fix it.

 



 Comments   
Comment by Dmitry Lukyanov (Inactive) [ 29/Sep/20 ]

Hello lefka.velchenko.work@gmail.com , thanks for your report.
You're right, this behavior was changed in the latest releases to prevent incorrect query generating which had been happened in the previous releases.
Unfortunately, we don't support LINQ queries where one LINQ level uses the LINQ parameters from the higher levels.
In your example, you use t LINQ parameter at the level where this parameter wasn't defined (the LINQ parameter at this level is l).
In 2.7.2 the driver did not handle this query correctly, even though it didn't throw an exception. Now we throw an exception to signal that this query is not supported.

Comment by Esha Bhargava [ 28/Sep/20 ]

lefka.velchenko.work@gmail.com Thanks for reporting this issue. We'll look into it and get back to you.

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