Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3603

ArgumentNullException when passing Expression to mongodb IQueryable

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.12.2
    • Component/s: Linq
    • None
    • Environment:
      Windows x64

      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.

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            kolka1993@gmail.com Николай Шматенков
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: