问题
I want to query my documents in my DocumentDB database. I want to use LINQ to handle the DocumentDB query and want to query for facebookUsername field.
If I use the code below for querying the standard Id field, it works fine but when I try to do it using facebookUsername field, I get a compile error that reads
"'Microsoft.Azure.Documents.Document' does not contain a definition for 'facebookUsername' and no extension method 'facebookUsername' accepting a first argument of type 'Microsoft.Azure.Documents.Document' could be found (are you missing a using directive or an assembly reference?)"
Here's the code I'm currently using for querying by Id and this works. I just want to be able to query the facebookUsername field.
dynamic doc = (from f in client.CreateDocumentQuery(collection.DocumentsLink)
where f.Id == myId.ToString()
select f).AsEnumerable().FirstOrDefault();
How do I modify my code to query by facebookUsername field?
回答1:
var families = from f in client.CreateDocumentQuery<Family>(colSelfLink)
where f.Address.City != "NY"
select f;
will give you a List where Family: { "Address" : {"City": "NY"} } }
if you don't have an object like Family, in my case, then you can't use Linq to evaluate queries on dynamic objects. You need to then use the SQL Query Grammar.
var families = client.CreateDocumentQuery<Family>(colSelfLink. "SELECT * FROM c WHERE field=value").AsEnumnerable();
should work.
来源:https://stackoverflow.com/questions/27028659/querying-documentdb-using-a-property-other-than-id