[CSHARP-4139] Unsupported filter x => x.SiteId.ToString().ToLower().Contains("634bb6fc") Created: 15/Apr/22  Updated: 27/Oct/23  Resolved: 05/May/22

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

Type: New Feature Priority: Unknown
Reporter: chock chen Assignee: Boris Dogadov
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File 1.PNG    

 Description   

Hi, I tried to retrieve using the expression and was prompted that it was not supported.

NOTE: The SiteId here is type of Guid

Following is my information:
1. Mongo driver: v2.15.0
2. LinqProvider.V3



 Comments   
Comment by chock chen [ 05/May/22 ]

Hi, boris.dogadov@mongodb.com

Thank you so much . It works in my side. I will go through the serialization guide. I think this issue can be resolved.

Comment by Boris Dogadov [ 02/May/22 ]

Hi chock-cong.chen@outlook.com 

For serializing GUID as a string you can use the following BSON attribute:

 

public class MyClass
{
   [BsonRepresentation(BsonType.String)]
   public Guid Field { get; set; }
}

 

And then x => x.Field.ToString().Containts(...)  should work.

To learn more about serialization please see the serialization guide.
You can also use our .Net forum for any related questions.

 

Comment by chock chen [ 25/Apr/22 ]

Hi, boris.dogadov@mongodb.com.
Thank you for your feedback. Actually, I followed your suggestion matching a substring would require serializing GUID as a string, but I got error from System.Linq.Expressions.

Expression<Func<Equipment, bool>> predicate = x => JsonConvert.SerializeObject(x.SiteId).Contains("89f456d9");

So about the above expression, could you provide an applicable solution.

Comment by Boris Dogadov [ 19/Apr/22 ]

Thank you for your question,

This behavior is expected. The problem is that by default GUIDs are stored as BinData, while $toString is not defined for BinData types.
LINQ3 surfaces this server error, while LINQ2 fails on the client side (the attached stacktrace shows that LINQ2 provider is used).

Matching a substring would require serializing GUID as a string. Matching the whole GUID field can be done directly:

var g = new Guid("7810d9d8-b5da-4f90-9077-16905ab78175");
Expression<Func<MyClass, bool>> f = x => x.GuidField == g;

 

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