[CSHARP-4370] Exception in .Where() matching Id Created: 17/Oct/22  Updated: 27/Oct/23  Resolved: 18/Oct/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.18.0
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Dimitri Kroo Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

Hi,

I'm getting an exception with simple filtering on ObjectId represented as string inside a C# class.

Exception:
MongoDB.Driver.Linq.ExpressionNotSupportedException: 'Expression not supported: x.SomeId in (x.SomeId == "634cf4c86a1bde31017xxxxx") because field "SomeId" is not represented as a string.'

Call:
someCollection.AsQueryable(options)
.Where(x => x.SomeId == "634cf4c86a1bde31017xxxxx");

Property in the class:
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string SomeId { get; set; }

It works with LinqProvider V2.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

2.18.0

How to Reproduce

Call AsQueryable with Where() filtering on objectId as string property inside a class.

Thank you!



 Comments   
Comment by Githook User [ 24/Oct/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4370: Confirm that sample code works with LINQ3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/b8756bc803b8fb91550f25a9878214ad18fed958

Comment by Robert Stam [ 18/Oct/22 ]

Thanks for letting us know that it is working for you.

Comment by Dimitri Kroo [ 18/Oct/22 ]

Thank you for support! My bad... It was a class without [BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]. Somebody forgot to add the attribute and it was working with V2. V3 is stricter I guess.

Best regards
Dimitri

Comment by Robert Stam [ 18/Oct/22 ]

I attempted to reproduce this and for me it worked correctly.

You can see my repro on the following branch and compare to see if I am doing anything differently than you:

https://github.com/rstam/mongo-csharp-driver/tree/csharp4370

Note: there IS an `ExpressionNotSupportedException` thrown internally, but it is deliberate and is caught and handled. If you have the debugger configured to stop when ANY exception is thrown (even if there is a catch handler for it) you might be seeing this internal exception.

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