Linq 使用Id以外的属性查询DocumentDB
我想在DocumentDB数据库中查询我的文档。我想使用LINQ处理DocumentDB查询,并想查询facebookUsername字段Linq 使用Id以外的属性查询DocumentDB,linq,azure,azure-cosmosdb,Linq,Azure,Azure Cosmosdb,我想在DocumentDB数据库中查询我的文档。我想使用LINQ处理DocumentDB查询,并想查询facebookUsername字段 dynamic doc = (from f in client.CreateDocumentQuery(collection.DocumentsLink) where f.Id == myId.ToString() select f).AsEnumerable().FirstOrDefault(); 如果我使用下面的代码来查询标准Id字段,它可
dynamic doc = (from f in client.CreateDocumentQuery(collection.DocumentsLink)
where f.Id == myId.ToString()
select f).AsEnumerable().FirstOrDefault();
如果我使用下面的代码来查询标准Id字段,它可以正常工作,但是当我尝试使用facebookUsername字段进行查询时,会出现一个编译错误
“'Microsoft.Azure.Documents.Document'不包含定义
对于“facebookUsername”和无扩展方法“facebookUsername”
接受类型为的第一个参数
找不到“Microsoft.Azure.Documents.Document”(您是否缺少
使用指令或程序集引用?“
这是我目前用于按Id查询的代码,它可以正常工作。我只想能够查询facebookUsername字段
dynamic doc = (from f in client.CreateDocumentQuery(collection.DocumentsLink)
where f.Id == myId.ToString()
select f).AsEnumerable().FirstOrDefault();
如何修改代码以按facebookUsername字段进行查询?var families=from f in client.CreateDocumentQuery(colSelfLink)
var families = from f in client.CreateDocumentQuery<Family>(colSelfLink)
where f.Address.City != "NY"
select f;
f地址城市在哪里!=“纽约”
选择f;
将给您一个家庭所在的列表:{“地址”:{“城市”:“纽约”}}
在我的例子中,如果没有像Family这样的对象,那么就不能使用Linq对动态对象的查询求值。然后需要使用SQL查询语法
var families = client.CreateDocumentQuery<Family>(colSelfLink. "SELECT * FROM c WHERE field=value").AsEnumnerable();
var families=client.CreateDocumentQuery(colSelfLink.“SELECT*FROM c,其中field=value”).asenumerable();
应该可以工作。尝试更改它以返回一个
IOrderedQueryable
来自client.CreateDocumentQuery(collection.DocumentsLink)中的f
我以前从未使用过IOrderedQueryable。你能详细说明它是什么,为什么它能解决我的问题吗?我真的很感谢你的帮助。非常感谢。我真正的意思是尝试将client.CreateDocumentQuery(collection.DocumentsLink)
更改为client.CreateDocumentQuery(collection.DocumentsLink)
。没有它,您将处理文档的可查询性,这些文档没有动态属性。我明白了。我将代码更改为:dynamic doc=(来自client.CreateDocumentQuery(collection.DocumentsLink)中的f,其中f.facebookUsername==myId.ToString()选择f);但是我现在得到了这个错误:“表达式树可能不包含动态操作”在这种情况下,您是否有一个表示doc db集合中的项的类(例如,一个包含facebook用户名
属性的类)?例如,如果您有一个名为User
的类,您应该能够使用client.CreateDocumentQuery(collection.DocumentsLink)
。我感谢你的帮助。