Mike
Mike

Reputation: 559

DocumentClient CreateDocumentQuery throws 'AsSQL' is not supported

I am trying to query a CosmosDB table for a record. I use the following syntax:

string sql = $"SELECT * FROM a i WHERE i.b='foobar'";

Then call:

    public async Task<bool> ExecuteQueryIdentity<T>(string collectionName, string sql)
    {
        if (database == null)
            database = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = DatabaseName });

        DocumentCollection collection = GetCollectionIfExists(collectionName);

        Uri collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, collectionName);

        var results = client.CreateDocumentQuery<T>(
            collectionUri,
            sql,
            DefaultOptions);

        return results != null && results.Count() > 0;            

    }

I get the following error:

Method 'AsSQL' is not supported. Only LINQ Methods are supported.

According to the Microsoft samples I can call using linq, lambda linq and sql. Is there a database or collection creation setting I need to set? What have I done wrong?

Upvotes: 1

Views: 108

Answers (1)

Mike
Mike

Reputation: 559

So I discovered the issue. On the last line of code I call:

results.Count()

The Count() is a linq call and the result set is choking on it. That's ok because I should write a better query so only the count is returned. However, this is clearly a bug with how the various APIs interact with each other.

I wonder if those that down voted me even understood that I found a legitimate bug in the API. If the two APIs do not support each other in this context IMHO this is a bug and there should at minimum be a compilation warning but probably an error.

Upvotes: 3

Related Questions