DocumentQueryException: Query expression is invalid

旧街凉风 提交于 2019-12-13 15:32:57

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/51734815/documentqueryexception-query-expression-is-invalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!