Home > Net >  Call CosmosDB ToString method in LINQ
Call CosmosDB ToString method in LINQ

Time:10-14

I have a LINQ query where I need to use the CosmosDB ToString method within a query that is built via LINQ in C#. However I have not been able to figure out how to do this. Simply calling ToString() in the C# LINQ expression on the entity value does not work.

I am trying to do a "contains" search on a number value. I can not change the type, and nor can I add a duplicated field that has the number converted to a string.

Essentially what I need is this (in the where clause):

CONTAINS(ToString(root["MyProperty"]), "MySearchValue")

My current LINQ query is this (MyProperty is of type long):

query.Where(x => x.MyProperty.ToString().Contains("MySearchValue"))

But this generates the following SQL:

CONTAINS(root["MyProperty"], "MySearchValue")

Which does not work since MyProperty is a number, so the Contains method always returns false.

So my question is this: How do I modify my LINQ query to wrap the number value in the ToString() method like in my first code sample above? Or is this even possible currently?

CodePudding user response:

There is actually a bugfix for this issue awaiting merge here: https://github.com/Azure/azure-cosmos-dotnet-v3/pull/3385 Does this apply to your specific situation?

  • Related