[CSHARP-4090] x => x.Name.Contains("FirstName", StringComparison.CurrentCultureIgnoreCase) throws NotSupportedException Created: 09/Mar/22  Updated: 28/Oct/23  Resolved: 15/Mar/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.14.1
Fix Version/s: 2.15.1

Type: New Feature Priority: Major - P3
Reporter: Chock chen Assignee: James Kovacs
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: CSHARP-3615
Backwards Compatibility: Minor Change

 Description   

Hi, when I trying to query LINQ request x => x.Name.Contains("FirstName", StringComparison.CurrentCultureIgnoreCase) ** throw the following error message.

 

Message:

Expression not supported: x.Name.Contains("FirstName", CurrentCultureIgnoreCase).

 

Following are my using

 - Mongo driver: v2.14.1

 ** - Linq provider: clientSettings.LinqProvider = LinqProvider.V3

NOTE: Original description described the problem that OrdinalIgnoreCase doesn't work. This is expected and explained below in the comments.

 



 Comments   
Comment by Githook User [ 04/May/22 ]

Author:

{'name': 'James Kovacs', 'email': 'jkovacs@post.harvard.edu', 'username': 'JamesKovacs'}

Message: CSHARP-4090 / CSHARP-4092 (#750)

CSHARP-4090: Implement support for string.Contains(match, StringComparison).
CSHARP-4092: Fix case-insensitive StartsWith/EndsWith to generate correct MQL.
Branch: v2.15.x
https://github.com/mongodb/mongo-csharp-driver/commit/f8bc005a1c866e13ac044dbe28708186b4bc184f

Comment by Githook User [ 15/Mar/22 ]

Author:

{'name': 'James Kovacs', 'email': 'jkovacs@post.harvard.edu', 'username': 'JamesKovacs'}

Message: CSHARP-4090 / CSHARP-4092 (#750)

CSHARP-4090: Implement support for string.Contains(match, StringComparison).
CSHARP-4092: Fix case-insensitive StartsWith/EndsWith to generate correct MQL.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/c8b95a34f282a76769890d335d35dd2f76659a42

Comment by chock chen [ 11/Mar/22 ]

Hi, james.kovacs  dmitry.lukyanov

Okay! Thank you for your feedback

 

Comment by James Kovacs [ 09/Mar/22 ]

Hi, 453532579@qq.com,

We have investigated the issue further and understand the root cause. A few wrinkles to mention...

We currently only support StringComparison.CurrentCultureIgnoreCase. To perform case-insensitive comparisons, we use $toLower, which only has well-defined behaviour on ASCII characters. While neither StringComparison.CurrentCultureIgnoreCase nor StringComparison.OrdinalIgnoreCase quite expresses this limitation, we chose to use StringComparison.CurrentCultureIgnoreCase as StringComparison.CurrentCulture is the default in .NET.

I've updated the title and description to reflect that StringComparison.CurrentCultureIgnoreCase throws a NotSupportedException when it shouldn't. We will continue to throw NotSupportedException for StringComparison.OrdinalIgnoreCase until the database supports ordinal comparisons.

The next wrinkle... string.Contains(string, StringComparison) is only available starting in .NET Standard 2.1. Thus it will only work if you are targeting .NET Core 3.X, .NET 5, or .NET 6. Notably it will not work with .NET Core 2.X nor .NET Framework. Interestingly you can compile a .NET Core 2.1 app (but not 2.0) using this overload, but NuGet packages target .NET Standard, not particular runtimes, and thus cannot provide support for this overload until .NET Standard 2.1 - which requires .NET Core 3.X or later.

Last wrinkle... While investigating this issue, we discovered a related issue CSHARP-4092. We have a fix for the CSHARP-4092 and have implemented support for string.Contains(string, StringComparison). It will be available in an upcoming release.

Sincerely,
James

Comment by Dmitry Lukyanov (Inactive) [ 09/Mar/22 ]

Hello 453532579@qq.com , thanks for your report, we will check it and come back to you.

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