2016-07-06 75 views
0

我想查詢從documentDB我的文件之一,但它似乎沒有奏效,這是我如何設置它:文檔DB查詢

NoSQLService:

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從INoSQLProvider

private readonly IDocumentClient client; 

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

AzureDocumentDBService繼承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); 

} 

當我運行測試我得到一個異常:

微軟,CSparp.RuntimeBuilder.RuntimeBinderException: '物體'd oes 不包含'Where'的定義。

回答

1

您在返回從NoSQLService.QueryDocumentdynamic,然後嘗試在其應用LINQ的擴展方法Where。你不能這樣做。您必須將其轉換爲可與該擴展配合使用的內容。

所以,無論是從使用動態

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

更改NoSQLService或在您的測試把結果

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

我都試過,但他們都拋出和異常:COM.PixelPin.NoSQLService .Tests.AzureDocumentDbTests.TestQueryDocument引發異常: System.InvalidCastException:無法將類型爲'Microsoft.Azure.Documents.Linq.DocumentQuery'1 [Microsoft.Azure.Documents.Document]'的類型的對象轉換爲鍵入'System.Linq.IQueryable '1 [COM.PixelPin.NoSQLService.Te sts.Documents]」。 – Callum

+0

剛注意到''Document'有兩種不同的類型。這是一個錯字嗎?我編輯了我的答案,以匹配應該從服務 – Nkosi

+0

工作,這一次是一個錯字。 – Callum