2
votes

I have below query (with complete code) not returning any data when there are in face rows that match that criteria. Are enums not supported in documentdb query?

        var query = _client.CreateDocumentQuery<Job>(CollectionUri, new FeedOptions()
        {
            MaxItemCount = 1
        })
        .Where(m => m.State != State.Done)
        .AsDocumentQuery();

        var docs = new List<Job>();
        while (query.HasMoreResults)
        {
            try
            {
                foreach (var p in await query.ExecuteNextAsync<Job>())
                {
                    docs.Add(p);
                }
            }
            catch (DocumentClientException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    return docs;
                }
                throw;
            }
        }
        return docs;

I just get docs as an empty list, no exceptions. State is an enum defined like below -

[JsonConverter(typeof(StringEnumConverter))]
public enum State
{
    ToProcess,
    Run,
    Done
}



 public class Job {
     [JsonProperty("id")]
     public string Id {get; set;}

     public State State {get;set;}
 }

EDIT: I tried changing where clause as below -

.Where(m => m.State.ToString() != State.Done.ToString())

But this doesn't work too and I get an error that "Method 'ToString' is not supported."

EDIT 2: corrected the typo where I inteded to say I had .ToString() in the EDIT above

EDIT 3: More information:

I am using default serialization to have camel casing in the documents as below

        JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };

this converts all the columns also in camel casing.

The actual query generated by LINQ is below -

{{"query":"SELECT * FROM root WHERE (root[\"State\"] != \"Done\") "}}

Hence there is column mismatch "State" vs "state".

Below is a sample document in document db

{
"id": "2017-03-02T22:00:00Z",
"state": "ToProcess"
}
2
Can you please post your class definition for Job? And what version of the DocumentDB .NET SDK are you using? - Aravind Krishna R.
Updated the class definition above. - Abhishek
I think I know the issue now - here is the query generated by LINQ - {{"query":"SELECT * FROM root WHERE (root[\"State\"] != \"Done\") "}} clearly - the column query is look for is pascal case "State" whereas the column in document is camel cased "state". Any way around that? - Abhishek

2 Answers

1
votes

According to your latest description, I assumed that you could change you model as follows:

public class Job {
     [JsonProperty("id")]
     public string Id {get; set;}
     [JsonProperty("state")]
     public State State {get;set;}
 }

RESULT

enter image description here

0
votes

Enumerated types are defined specifically within your app. You'll need to convert them to a string (or numeric value) that matches what's in the database, since enums are not a data type within DocumentDB.