我有一個用於DocumentDB文檔的.NET POCO類。使用.NET DocumentDB SDK查詢對象屬性
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集合中的所有文檔實際上是FatDocument的JSON序列化版本。
對於我的業務應用程序中的很多目的,我不需要檢索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>
。
如果我創建一個QueryDocument
與client.CreateDocumentQuery<LightDocument>
,執行後,此IQueryable返回一個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查詢?
JamyRyals有這個最佳的解決方案 - 通過使用投影選擇。我還建議檢查一下這個變化是否對RU有重大改變。一般來說,附加屬性的投影不會在RU中造成很大的差異。 –
根據請求單元,我獲得了x2增益! –