TransWikia.com

Cosmos DB Error with Member Indexer when mapping value to a class model in select

Stack Overflow Asked by AndyBufNY on December 9, 2021

I’m having issue with Cosmos DB where it throws an error if I try to map an indexed member to a property on a class. However it works fine if I switch the query to map to an anonymous type.

Some Code to set the stage

CosmosClient client = new CosmosClient(connectionString);
var container = client.GetContainer(DatabaseId, CollectionId);
var q = container.GetItemLinqQueryable<Entity>(allowSynchronousQueryExecution: true);
var testName = "Test";

//Class used in mapping for select 
public class Model{
    public string Name { get; set; }
}

//Sample CosmosDB document entity structure
public class Entity
{
    public Child Child { get; set; }
}
public class Child
{
    public string Name { get; set; }
    public Dictionary<string, Child> NamedChildren { get; set; }
}

This will work

var items = q.Select(q => new { Name = q.Child.NamedChildren[testName].Name }).ToList();

And produces this working sql

SELECT VALUE {"Name": root["Child"]["NamedChildren"]["Test"]["Name"]} FROM root

This will NOT work

If I map the select to the model class (see "new Model" below) it will throw an error

var items = q.Select(q => new Model { Name= q.Child.NamedChildren[testName].Name }).ToList()

Produces this incorrect sql

SELECT VALUE {"Name": root["Child"]["NamedChildren"][{"testName": "Test"}["testName"]]["Name"]} FROM root

and this error from Cosmos DB

Microsoft.Azure.Cosmos.Query.Core.Exceptions.ExpectedQueryPartitionProviderException:

{
"errors":[
{
"severity":"Error",
"location":
{
"start":22,
"end":86
},
"code":"SC2990",
"message":"The specified query includes 'member indexer' which is currently not supported."}
]
}

Using a Const Changes the outcome though

If I change

var testName = "test";

to be

const string testName = "test";

Then the code that is not working works and the ‘sql’ changes to the expected

SELECT VALUE {"Name": root["Child"]["NamedChildren"]["Test"]["Name"]} FROM root

I could fill the collection of anonymous types and then map to my strong typed collection, but obviously not ideal.

I’m having trouble understanding why it would fail as I would have thought the class mapping happened after the query was finished and returned. However the actual query/SQL is different.

Is this a bug or is there something I’m missing as to why this should fail or work differently.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP