[CSHARP-3603] ArgumentNullException when passing Expression to mongodb IQueryable Created: 22/Apr/21  Updated: 22/Apr/21  Resolved: 22/Apr/21

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

Type: Bug Priority: Unknown
Reporter: Николай Шматенков Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows x64


Issue Links:
Duplicate
duplicates CSHARP-2422 Dynamic Linq expression with Paramete... Closed

 Description   

If I pass common expression to Where clause to IMongoQueryable it returns ArgumentNullException (Value cannot be null. (Parameter 'itemName')).

 

How to reproduce:

public class TestEntity : Entity
    {
    }    public static class MongoBugs
    {
        [Fact]
        public static void TestMongoIQueryableBug()
        {
            var mongoDb = new MongoClient(new MongoClientSettings
            {
                ServerSelectionTimeout = TimeSpan.FromMilliseconds(0)
            }).GetDatabase("admin");
            var expression = CreateGuidFilterExpression(nameof(TestEntity.Id), Guid.Empty);
            try
            {
                mongoDb.GetCollection<TestEntity>(nameof(TestEntity)).AsQueryable().Where(expression).First();
            }
            catch (Exception ex)
            {
                ex.Should().BeAssignableTo<ArgumentNullException>();
            }
        }        
 
private static Expression<Func<TestEntity, bool>> CreateGuidFilterExpression(string propertyName,
            Guid value)
        {
            // expression input parameter
            var entityParameter = Expression.Parameter(typeof(TestEntity));
            return
                // parameter.[propertyName] == .Constant(value) expression
                Expression.Lambda<Func<TestEntity, bool>>(
                    Expression.Equal(Expression.Property(entityParameter, propertyName),
                        Expression.Constant(value)),
                    entityParameter);
        }
    }

If I write

var entityParameter = Expression.Parameter(typeof(TestEntity), "entity");

it works.

 



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

Hello kolka1993@gmail.com,
Thank you for reporting this issue.
This appears to be a duplicate of CSHARP-2422. Please follow that ticket.
 

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

public abstract class Entity
    {
        [BsonId(IdGenerator = typeof(GuidGenerator))]
        [BsonRepresentation(BsonType.String)]
        public Guid Id { get; set; } = default;
}

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