在EF中,如果要在查詢結果中包含導航屬性,請使用Include()
。由於一些查詢需要調用其再次,我試圖創建一個圍繞這一概念的通用包裝:使用linq表達式的params數組
public IQueryable<T> FindAll<P>(params Expression<Func<T, P>>[] predicates) where P : class {
var entities = AllEntities();
foreach (var p in predicates) entities = entities.Include(p);
return entities;
}
並調用它是這樣:
var customers = FindAll(q => q.Orders, q => q.Invoices, q => q.Contacts);
問題:
- 的函數編譯,但是當我調用它時:「方法的類型參數不能從用法推斷,請嘗試明確指定類型參數。」我究竟做錯了什麼?
- 如果我得到它的工作,我可以在第一時間撥打
Include()
:var customers = FindAll(q => q.Orders, q => q.Invoices);
然後再在一個單獨的步驟:customers = customers.Include(p => p.Invoices);
或將導致表現差,這樣我應該一次做包括?
編輯:
JonSkeet的答案是正確的,當然,仍然there is this solution這似乎做我想做的。不知道爲什麼它能正常工作。是否因爲Aggregate()
功能?
會使它成爲'Func'嗎? –