Details
-
Bug
-
Resolution: Duplicate
-
Unknown
-
None
-
2.12.2
-
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
Issue Links
- duplicates
-
CSHARP-2422 Dynamic Linq expression with ParameterExpression would throw value can not be null Exception if ParameterName not set
-
- Closed
-