使用 .NET DocumentDB SDK 查询对象属性
Querying objects properties with .NET DocumentDB SDK
我的 DocumentDB 文档有一个 .NET POCO class。
public class FatDocument{
public string id { get; set; }
public string LightProperty { get;set;}
public string LightProperty2 { get;set;}
public string FatProperty { get;set;}
}
顾名思义,FatProperty包含了Document的大部分信息。 我的 DocumentDB 集合中的所有文档实际上都是 JSON FatDocument.
的序列化版本
对于我的业务应用程序中的许多用途,我不需要检索 FatProperty。 为了节省一些RU,我制作了一个轻量版的POCO
public class LightDocument{
public string id { get; set; }
public string LightProperty { get;set;}
public string LightProperty2 { get;set;}
}
public class FatDocument: LightDocument{
public string FatProperty { get;set;}
}
现在我正在寻找一种方法来检索 IQueryable<LightDocument>
。
如果我用client.CreateDocumentQuery<LightDocument>
创建一个QueryDocument
,执行后,这个IQueryablereturns一个LightDocument枚举。但是在 DocumentDB 请求检查之后,我们看到了 SELECT * FROM
。这不是我们要找的,我们想忽略 DocumentDB 查询中的 FatProperty
以保存一些 RU(以及客户端应用程序和 DocumentDB 之间的请求负载)。
我还尝试使用 SQL 语法组合查询的创建
var queryspec = new SqlQuerySpec() { QueryText = "SELECT c.id, c.LightProperty, c.LightProperty2"};
var queryable = client.CreateDocumentQuery<LightDocument>(GetDocUri(), queryspec);
queryable.Where(c => /* Some filtering logic */);
queryable.AsEnumerable(); //EXCEPTION thrown
这会引发异常 Method 'AsSQL' is not supported. Only LINQ Methods are supported
。
注意: 反转 AsEnumerable 和 Where 在这里不是一个选项。我们希望将 where
子句转换为 DocumentDB where 子句。
我的问题是:如何使用返回部分文档的 DocumentDB .NET SDK 创建 LINQ 查询?
如果您使用 LINQ 而不是 SQL 查询,您可以使用 IQueriable 上的 .Select() 方法将胖文档投影到轻文档。然后您可以将结果链接到 .Where() 方法。
轻文档查询
var lightDocumentQuery = client.CreateDocumentQuery<LightDocument>(GetCollectionLink())
.Select(d => new LightDocument { id = d.id, LightProperty = d.LightProperty, LightProperty2 = d.LightProperty2 })
.Where(d => d.LightProperty == compareTo)
.AsDocumentQuery();
轻文档结果
{
"id": "9d4ec687-95a5-68df-d51d-5d2fb0143231",
"LightProperty": "someValue",
"LightProperty2": "someOtherValue"
}
胖文件查询
var fatDocumentQuery = client.CreateDocumentQuery<FatDocument>(GetCollectionLink())
.Where(d => d.LightProperty == compareTo)
.AsDocumentQuery();
胖文件结果
{
"FatProperty": "SomeFatProperty usually json",
"id": "9d4ec687-95a5-68df-d51d-5d2fb0143231",
"LightProperty": "someValue",
"LightProperty2": "someOtherValue"
}
轻型文档示例中生成的查询未引用 FatProperty,因此未通过网络发送。我继续检查每种请求类型的 RU,它们几乎是均匀的,FatDocument 查询的成本略高,这是合理的,因为使用的带宽更多。
- LightDocument 查询 RU:3.05
- FatDocument 查询 RU:3.09
我的 DocumentDB 文档有一个 .NET POCO class。
public class FatDocument{
public string id { get; set; }
public string LightProperty { get;set;}
public string LightProperty2 { get;set;}
public string FatProperty { get;set;}
}
顾名思义,FatProperty包含了Document的大部分信息。 我的 DocumentDB 集合中的所有文档实际上都是 JSON FatDocument.
的序列化版本对于我的业务应用程序中的许多用途,我不需要检索 FatProperty。 为了节省一些RU,我制作了一个轻量版的POCO
public class LightDocument{
public string id { get; set; }
public string LightProperty { get;set;}
public string LightProperty2 { get;set;}
}
public class FatDocument: LightDocument{
public string FatProperty { get;set;}
}
现在我正在寻找一种方法来检索 IQueryable<LightDocument>
。
如果我用client.CreateDocumentQuery<LightDocument>
创建一个QueryDocument
,执行后,这个IQueryablereturns一个LightDocument枚举。但是在 DocumentDB 请求检查之后,我们看到了 SELECT * FROM
。这不是我们要找的,我们想忽略 DocumentDB 查询中的 FatProperty
以保存一些 RU(以及客户端应用程序和 DocumentDB 之间的请求负载)。
我还尝试使用 SQL 语法组合查询的创建
var queryspec = new SqlQuerySpec() { QueryText = "SELECT c.id, c.LightProperty, c.LightProperty2"};
var queryable = client.CreateDocumentQuery<LightDocument>(GetDocUri(), queryspec);
queryable.Where(c => /* Some filtering logic */);
queryable.AsEnumerable(); //EXCEPTION thrown
这会引发异常 Method 'AsSQL' is not supported. Only LINQ Methods are supported
。
注意: 反转 AsEnumerable 和 Where 在这里不是一个选项。我们希望将 where
子句转换为 DocumentDB where 子句。
我的问题是:如何使用返回部分文档的 DocumentDB .NET SDK 创建 LINQ 查询?
如果您使用 LINQ 而不是 SQL 查询,您可以使用 IQueriable 上的 .Select() 方法将胖文档投影到轻文档。然后您可以将结果链接到 .Where() 方法。
轻文档查询
var lightDocumentQuery = client.CreateDocumentQuery<LightDocument>(GetCollectionLink())
.Select(d => new LightDocument { id = d.id, LightProperty = d.LightProperty, LightProperty2 = d.LightProperty2 })
.Where(d => d.LightProperty == compareTo)
.AsDocumentQuery();
轻文档结果
{
"id": "9d4ec687-95a5-68df-d51d-5d2fb0143231",
"LightProperty": "someValue",
"LightProperty2": "someOtherValue"
}
胖文件查询
var fatDocumentQuery = client.CreateDocumentQuery<FatDocument>(GetCollectionLink())
.Where(d => d.LightProperty == compareTo)
.AsDocumentQuery();
胖文件结果
{
"FatProperty": "SomeFatProperty usually json",
"id": "9d4ec687-95a5-68df-d51d-5d2fb0143231",
"LightProperty": "someValue",
"LightProperty2": "someOtherValue"
}
轻型文档示例中生成的查询未引用 FatProperty,因此未通过网络发送。我继续检查每种请求类型的 RU,它们几乎是均匀的,FatDocument 查询的成本略高,这是合理的,因为使用的带宽更多。
- LightDocument 查询 RU:3.05
- FatDocument 查询 RU:3.09