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

ArgumentNullException when passing Expression to mongodb IQueryable

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Unknown Unknown
    • None
    • 2.12.2
    • Linq
    • None
    • Windows x64

    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.

       

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: