Querying DocumentDB using a property other than Id

点点圈 提交于 2020-01-01 12:01:05

问题


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

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