Reputation: 73
I'm trying to perform a case insensitive search using the C# Cosmos SDK, v3.11.0. From reading the CosmosDB blog post the SQL API now supports this - but I can't get it to work using the SDK. The release notes for 3.10.0 would suggest this is supported but I can't find any documentation on how to use it.
I've tried the following code but it does not perform a case-insensitive search:
query.Where(d => d.Name.Contains(name, StringComparison.InvariantCultureIgnoreCase));
Using the following code results in an exception being thrown:
query.Where(d => d.Name.StartsWith(name, StringComparison.InvariantCultureIgnoreCase))
Microsoft.Azure.Cosmos.Linq.DocumentQueryException: Method 'StartsWith' is not supported., Windows/10.0.18363 cosmos-netstandard-sdk/3.11.2
at Microsoft.Azure.Cosmos.Linq.BuiltinFunctionVisitor.Visit(MethodCallExpression methodCallExpression, TranslationContext context)
at Microsoft.Azure.Cosmos.Linq.StringBuiltinFunctions.Visit(MethodCallExpression methodCallExpression, TranslationContext context)
at Microsoft.Azure.Cosmos.Linq.BuiltinFunctionVisitor.VisitBuiltinFunctionCall(MethodCallExpression methodCallExpression, TranslationContext context)
at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitNonSubqueryScalarExpression(Expression inputExpression, TranslationContext context)
at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitNonSubqueryScalarExpression(Expression expression, ReadOnlyCollection`1 parameters, TranslationContext context)
at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitScalarExpression(Expression expression, ReadOnlyCollection`1 parameters, TranslationContext context)
at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitScalarExpression(LambdaExpression lambda, TranslationContext context)
at Microsoft.Azure.Cosmos.Linq.ExpressionToSql.VisitWhere(ReadOnlyCollection`1 arguments, TranslationContext context)
Upvotes: 5
Views: 3372
Reputation: 11
Equals works too btw (at least in version 3.31.2):
query.Where(t => t.Description.Equals(searhTerm, StringComparison.InvariantCultureIgnoreCase));
Upvotes: 1
Reputation: 26956
Case Insensitive support is definitely in SDK 3.12.0:
string searchTerm = "do something";
var query = container.GetItemLinqQueryable<ToDoItem>();
query.Where(t => t.Description.StartsWith(searhTerm, StringComparison.InvariantCultureIgnoreCase));
Will run successfully, and return results where Description starts with things like "Do Something" or "do something".
Upvotes: 2
Reputation: 18526
Support for the syntax you wrote is currently in the works. A pull request was started just a couple of hours ago.
Until then, the queries below work with the current SDK version (3.11), but they also should work with older versions. It should be considered a workaround for case insensitivity. The RU cost is higher than the native case insensitive search. I did a quick test, and the cost of ToLower().StartsWith(...)
was almost twice the cost of the SQL equivalent.
string mySearchStringLowered = name.ToLower();
//...
query.Where(d => d.Name.ToLower() == mySearchStringLowered);
query.Where(d => d.Name.ToLower().StartsWith(mySearchStringLowered));
Upvotes: 4
Reputation: 8763
That is not the correct syntax. Please see the documentation here
Upvotes: 0
Reputation: 108975
StringEquals
is not yet supported in the SDK (yet). I found a message from someone in the Cosmos DB team confirming this (and noting the communication could be better).
I've also found it is not supported in the emulator either.
Upvotes: 1