2

I have updated my project with the MongoDb C# Driver 1.4 and one of my Lambda expression is not working anymore.

Before I was using MongoDb C# Driver 1.3.1 with Fluent Mongo to support Linq.

Here is my method:

IQueryable<T> IBackend<T>.Get(System.Linq.Expressions.Expression<Func<T, bool>> expression)
{
    return collection.AsQueryable<T>().Where(expression);
}

This lambda expression works:

var addedCustomer = repo.Get(c => c.FirstName == "Elwood").SingleOrDefault();

This one now throws an exception:

var updatedCustomer = repo.Get(c => c.Id == customer.Id).SingleOrDefault();

Thrown exception message:

Object reference not set to an instance of an object.

Update here is my stacktrace:

MongoDB.Bson.dll!MongoDB.Bson.Serialization.BsonClassMapSerializer.GetMemberSerializationInfo(string memberName) Line 253 + 0x3 bytes   C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.GetSerializationInfoMember(MongoDB.Bson.Serialization.IBsonSerializer serializer, System.Linq.Expressions.MemberExpression memberExpression) Line 962 + 0xc bytes    C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.GetSerializationInfo(MongoDB.Bson.Serialization.IBsonSerializer serializer, System.Linq.Expressions.Expression expression) Line 888 + 0xf bytes  C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.GetSerializationInfo(System.Linq.Expressions.Expression expression) Line 880 + 0xf bytes C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.BuildComparisonQuery(System.Linq.Expressions.BinaryExpression binaryExpression) Line 433 + 0x1f bytes    C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.BuildQuery(System.Linq.Expressions.Expression expression) Line 768 + 0x37 bytes  C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.BuildQuery() Line 113 + 0xc bytes    C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.Execute() Line 122 + 0x9 bytes   C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.MongoQueryProvider.Execute(System.Linq.Expressions.Expression expression) Line 147 + 0xb bytes   C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.MongoQueryProvider.Execute<Lion.Tools.Tests.Backends.Entities.Customer>(System.Linq.Expressions.Expression expression) Line 131 + 0xc bytes  C#
[External Code] 
Lion.Tools.Tests.dll!Lion.Tools.Tests.Backends.MongoDbBackendTests.MongoDb_Can_Add_Select_And_Update_Test() Line 79 + 0x27f bytes   C#
[External Code] 

Any idea on what's going wrong?

Thanks

4
  • 1
    Have you verified that customer is not null? Commented Apr 11, 2012 at 14:15
  • 1
    You should add the stack trace, knowing where the exception originates is very useful information. Commented Apr 11, 2012 at 14:26
  • Well, based on the stack trace you provided, it looks like the Bson serializer is attempting to build the query and serialize the lambda expression, but is unable to do so. Depending on the actual implementation there could be several causes to this, depending how they implemented it. Id suggest contacting MongoDB directly Commented Apr 11, 2012 at 14:44
  • Ok, I'll check that with 10gen... will post solution here when I have it Commented Apr 11, 2012 at 14:47

3 Answers 3

6

There is a bug in the 1.4 version of the C# driver that affects LINQ queries against inherited properties:

https://jira.mongodb.org/browse/CSHARP-418

This has been fixed in the master branch and the fix will be in the 1.4.1 release which we plan to release soon.

Sign up to request clarification or add additional context in comments.

1 Comment

Please make that soon sooner :-) But, as a work around, one can just implement the Property in the Top class. Make the BsonClassMap skip the base property. ( I can do this cause my WebApp only reads, it would ofcause give other problems if write is to be supported.
0

There is nothing that is obviously wrong. You need to check several things:

  1. Is it really throwing at the line you showed us?
  2. Is repo not null ?
  3. Is customer not null, e.g. does it work if you replace customer.Id with a fixed value like 1?
  4. Make sure repo.Get(...) never returns null.

1 Comment

1. the exception is thrown when the Linq query is evaluated, 2. repo is not null, 3. customer is not null... The query fails only when I do my lambda expression on Id... on FirstName it works
0

Not much to help but, Where expression usually return something or throws a NotFound. In that case, this mean that either repo or customer is null.

If you think that updating the provider caused this, try to check the Where return value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.