文档数据库查询

Document DB query

我正在尝试从 documentDB 查询我的一个文档,但它似乎不起作用,我是这样设置的:

NoSQL服务:

private INoSQLProvider client;
private static IContainer container;

public NoSQLService(INoSQLProvider provider)
{
    client = provider;
}

public static dynamic QueryDocument<T>(Uri reference, FeedOptions queryOptions)
{
    var documentDB = container.Resolve<INoSQLProvider>();
    return documentDB.QueryDocumentAsync(reference, queryOptions);
}

INoSQLProvider:

IOrderedQueryable<Document> QueryDocumentAsync(Uri reference, FeedOptions queryOptions);

AzureDocumentDBService

AzureDocumentDBService 继承自 INoSQLProvider

private readonly IDocumentClient client;

public IOrderedQueryable<Document> QueryDocumentAsync (Uri reference, FeedOptions queryOptions)
{
    return client.CreateDocumentQuery(reference, queryOptions);
}

AzureDocumentDBTest

FeedOptions queryOptions = new FeedOptions();

Documents document = new Documents();

document.id = "112";

queryOptions.MaxItemCount = -1;

var reference = NoSQLService.CreateDocumentUri("ppsession1", "callumtest");
IQueryable<Documents> documentQuery = NoSQLService.QueryDocument<Documents>(reference, queryOptions).Where(document.id = "112");

foreach (Documents documents in documentQuery)
{
    Console.WriteLine("\tRead {0}", documents);

}

当我 运行 测试时出现异常:

Microsoft,CSparp.RuntimeBuilder.RuntimeBinderException: 'object' does not contain a definition for 'Where'.

您正在从 NoSQLService.QueryDocument 返回 dynamic,然后尝试对其应用 Linq 扩展方法 Where。这是你做不到的。您必须将其转换为可以与该扩展一起使用的东西。

所以要么改变你的NoSQLService使用动态

public static IQueryable<Document> QueryDocument<T>(Uri reference, FeedOptions queryOptions)
{
    var documentDB = container.Resolve<INoSQLProvider>();
    return documentDB.QueryDocumentAsync(reference, queryOptions);
}

或将结果投射到您的测试中

var reference = NoSQLService.CreateDocumentUri("ppsession1", "callumtest");
IQueryable<Document> documentQuery = ((IQueryable<Document>)NoSQLService.QueryDocument<Document>(reference, queryOptions)).Where(document => document.id == "112");