使用Id以外的属性查询DocumentDB [英] Querying DocumentDB using a property other than Id

查看:64
本文介绍了使用Id以外的属性查询DocumentDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询我的DocumentDB数据库中的文档.我想使用LINQ来处理DocumentDB查询,并要查询facebookUsername字段.

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.

如果我使用下面的代码查询标准ID字段,它可以正常工作,但是当我尝试使用facebookUsername字段进行操作时,出现读取错误

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'不包含定义 对于"facebookUsername",没有扩展方法"facebookUsername" 接受类型的第一个参数 可以找到"Microsoft.Azure.Documents.Document"(您是否缺少 使用指令还是程序集引用?)"

"'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?)"

这是我当前用于通过ID查询的代码,此代码有效.我只想能够查询facebookUsername字段.

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();

如何修改我的代码以按facebookUsername字段进行查询?

How do I modify my code to query by facebookUsername field?

推荐答案

var families = from f in client.CreateDocumentQuery<Family>(colSelfLink)
           where f.Address.City != "NY"
           select f;

将为您提供以下列表:家庭:{地址":{城市":"NY"}}}

will give you a List where Family: { "Address" : {"City": "NY"} } }

如果没有像Family这样的对象,那么就不能使用Linq来评估对动态对象的查询.然后,您需要使用SQL查询语法.

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();

应该工作.

这篇关于使用Id以外的属性查询DocumentDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆