[CSHARP-2422] Dynamic Linq expression with ParameterExpression would throw value can not be null Exception if ParameterName not set Created: 25/Oct/18  Updated: 28/Oct/23  Resolved: 25/Oct/21

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

Type: Improvement Priority: Major - P3
Reporter: elendil Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on CSHARP-3648 LINQ3: Handle null ParameterExpressio... Closed
Duplicate
is duplicated by CSHARP-3603 ArgumentNullException when passing Ex... Closed

 Description   

Dynamically create a Expression, Define the parameter of the Expression as below

var parameter = Expression.Parameter(typeof(T));

then pass this expression to MongoQueryable.Where function

call Count or ToList

reported the below exception

Value cannot be null.
Parameter name: itemName

 

on the other hand, if i declare the parameter of the expression as below

var parameter = Expression.Parameter(typeof(T),"item");

then the linq expression can be successfully transformed to mongodb query

 

while transform the linq expression to mongodb query, why does the parameter name is required? when it comes to dynamically generated expression people usually use anonymous parameter and the exception message does not explicitly indicate the parameter expression need a name. 



 Comments   
Comment by Switchs89 N/A [ 25/Oct/21 ]

Thanks!

Comment by Robert Stam [ 25/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 [ 25/Oct/21 ]

Author:

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

Message: CSHARP-2422: Verify that dynamic predicates with null parameterNames work in LINQ3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/ec5da32d3418f87cd158705caeec9d3dd741b02a

Comment by Robert Stam [ 04/May/21 ]

It's a surprise to us that `ParameterExpression.Name` can be `null`, but looking at the documentation I agree with you that it is legal for `Name` to be `null`.

We are currently writing a new LINQ provider. We won't retrofit the existing LINQ provider to support `null` parameter names, but we will make sure this is supported by the new LINQ provider.

Comment by Switchs89 N/A [ 22/Apr/21 ]

I agree with Николай Шматенков. The inconsistency in behavior of LINQ translator indicates a bug, rather than a designed feature.

I also believe that providing parameter name when creating a parameter expression is not required for the expression to be correct and suitable for translating. Other query providers like EF Core use their own parameter names anyway.

 The documentation about name parameter in Expression.Parameter(Type type, string name) method:

> The name of the parameter or variable, used for debugging or printing purpose only. ~ Docs

Comment by Николай Шматенков [ 22/Apr/21 ]

But if I pass same expression into a where clause of some filter builder it works fine. What means - this is a bug, not a designed feature.

Comment by Mikalai Mazurenka (Inactive) [ 22/Apr/21 ]

Our LINQ translator handles this case just fine if you use expression lambda.
In case you want to build the expression yourself, be sure it is correct and equivalent to what expression lambda would create.
For this particular case that means explicitly specifying the parameter name.

Comment by Switchs89 N/A [ 22/Apr/21 ]

Hi,

I've also came across this bug. I also confirm that explicitly specifying the parameter name works as a workaround this problem.

Here is a full repro

https://gist.github.com/Prologh/164ca4ccf24423d70c2087aeb397430f

Comment by Hunain Durrani [ 31/Oct/19 ]

Hi,

I am also having this problem i am trying to create a daynamic where clause for my query by using expression. if i create ParameterExpression variable using Expression.Parameter(typeof(MyObject)); i get the error "Value cannot be null. (Parameter 'itemName')" on ToListAsync but if i create it Expression.Parameter(typeof(VanInformation),"Abc"); then i dont get error but also my filter doenot work. can anyone help me to solve this problem.

Comment by Jeffrey Yemin [ 07/Dec/18 ]

As we haven't heard back from you, we're closing this issue. If you are able to provide more details, we'll be happy to re-open it.

Comment by Robert Stam [ 05/Nov/18 ]

Can you please provide sample code that can be compiled and run to reproduce this with?

We're not quite sure from your description how to reproduce this.

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