Luis Valencia
Luis Valencia

Reputation: 33988

DocumentQueryException: Query expression is invalid

I need to query a collection in cosmosdb.

My entity is:

 public class Tenant
    {
        public string Id { get; set; }

        public string ClientId { get; set; }

        public string ClientSecret { get; set; }

        public string TenantDomainUrl { get; set; }

        public bool Active { get; set; }

        public string SiteCollectionTestUrl { get; set; }


        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }

And my controller action is:

 [HttpGet]
        [Route("api/Tenant/GetActiveTenant")]
        public Tenant GetActiveTenant()
        {
            var tenantStore = CosmosStoreFactory.CreateForEntity<Tenant>();
            return tenantStore.Query().Where(x => x.Active == true).FirstOrDefault();
        }

However when trying to use this endpoint, I get this error

DocumentQueryException: Query expression is invalid, expression https://cosmosdb-app-centralus.documents.azure.com/dbs/ToDoList/colls/tenants.Where(x => (x.Active == True)).FirstOrDefault() is unsupported. Supported expressions are 'Queryable.Where', 'Queryable.Select' & 'Queryable.SelectMany' emphasized text

I am using cosmonaut nuget package.

The only document I have in the collection:

{
    "ClientId": "aaaaaaaa-4817-447d-9969-e81df29c813d",
    "ClientSecret": "aaaaaaaaaaaaaaaaaa/esrQib6r7FAGd0=",
    "TenantDomainUrl": "abc.onmicrosoft.com",
    "SiteCollectionTestUrl": "https://abc.sharepoint.com/sites/Site1",
    "Active": true,
    "id": "d501acc6-6b63-4f0f-9782-1473af469b56",
    "_rid": "kUZJAOPekgAEAAAAAAAAAA==",
    "_self": "dbs/kUZJAA==/colls/kUZJAOPekgA=/docs/kUZJAOPekgAEAAAAAAAAAA==/",
    "_etag": "\"00002602-0000-0000-0000-5b69fe790000\"",
    "_attachments": "attachments/",
    "_ts": 1533673081
}

Upvotes: 2

Views: 218

Answers (1)

Nick Chapsas
Nick Chapsas

Reputation: 7200

As Cosmonaut's ReadMe page states, you should be using the Async method extensions for Cosmonaut because they will go properly though the SDK's async flow.

For example in your case, you should change your code to await tenantStore.Query().Where(x => x.Active == true).FirstOrDefaultAsync();

PS: You should also consider adding the [JsonAttribute("id")] attribute to your Id property. Even though it's not needed, it is recommended especially if you want to do querying based on the Id.

Upvotes: 3

Related Questions