Azure / azure-cosmos-dotnet-v3

.NET SDK for Azure Cosmos DB for the core SQL API
MIT License
739 stars 494 forks source link

Convert enum to strings in LINQ query #3491

Closed giovanni-ferrari closed 9 months ago

giovanni-ferrari commented 2 years ago

Is your feature request related to a problem? Please describe. Trying to execute a LINQ query on IOrderedQueriable does not allow to query enums as strings

Describe the solution you'd like

this.container.GetItemLinqQueryable<Order>() .Where(o => o.State == OrderState.Completed)

translates to {"query":"SELECT VALUE root FROM root WHERE (root[\"state\"] = \"Completed\")"} instead of {"query":"SELECT VALUE root FROM root WHERE (root[\"state\"] = 1)"}

Additional context It would be possible to allow to add a JsonStringEnumConverter in CosmosLinqSerializerOptions.Converters. or better to use the CosmosSerializer used in CosmosClient

ealsur commented 2 years ago

@leminh98 @adityasa Could you take a look at this issue?

wrkntwrkn commented 2 years ago

this might be related to https://github.com/Azure/azure-cosmos-dotnet-v3/issues/1063

kskem commented 1 year ago

@giovanni-ferrari Similar with additional context in #3280

Maya-Painter commented 9 months ago

Hi all, we've merged what we hope will be a solution to this issue by allowing the custom serializer to be used for LINQ translations. This also unblocks having System.Text.Json attributes applied in LINQ queries.

We plan to release this as part of the next preview package, but I also wanted to take this time to confirm these changes meet your needs before finalizing them as part of our public API. If you try the changes out, please follow up in the thread if this update addresses your concerns. If it doesn't, please let us know why.

To use your own custom serializer for LINQ translations you must implement CosmosLinqSerializer and set it as a custom serializer on the CosmosClient. There is a sample of this for serialization with System.Text.Json in CosmosLinqSerializer.cs.

Barsonax commented 6 months ago

Still seems to be broken in the latest 3.81.1 nuget package. Does this fix stil have to be released?

Maya-Painter commented 6 months ago

Hi @Barsonax, it will be released in the 3.39.0 package, which should be available within a few days.

cultpodcasts commented 2 months ago

Leaving a link to source mentioned in comment above https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs