Elfocrash / Cosmonaut

🌐 A supercharged Azure CosmosDB .NET SDK with ORM support
https://cosmonaut.readthedocs.io
MIT License
342 stars 44 forks source link

How to avoid aggregate / distinct result casted to list of entities of store instead of actual value #101

Closed DennisBaekgaard closed 4 years ago

DennisBaekgaard commented 4 years ago

If you have the following store:

services.AddCosmosStore<MyEntity>(cosmosSettings);

And use it in the following way: var result = await _store.Query("SELECT DISTINCT VALUE c.id FROM c").ToListAsync();

The output from the query is in the form of a list of strings, but cosmonaut tries to cast it to a list of MyEntity.

I was looking through the source, but at least I couldn't find a way to prevent this. Is there a away I can type-cast the expected response to something else than the registered store value?

Basically what I'm trying to do is just counting the distinct amount of entities of type T in cosmos. I get I could just query the actual list of entities and then do the aggregates on that list, but the performance of in-cosmos aggregations are a lot better.

Elfocrash commented 4 years ago

Hello @DennisBaekgaard,

If you need anything other than the entity itself then you shouldn't be using the Query method. You should use the QueryMultipleAsync<T> instead.

Here is how your query should be: var result = await _store.QueryMultipleAsync<string>("SELECT DISTINCT VALUE c.id FROM c");

DennisBaekgaard commented 4 years ago

Hi @Elfocrash

As per usual superb response time and answer. Thanks - I'll see if I can find the time to update your docs a bit for some more clarity if you don't mind.

Elfocrash commented 4 years ago

No problem. Yeah please do. I always forget to document some stuff, 😭. Feel free to close this.