2016-05-30 58 views
1

我想獲得的所有對象,其中孩子的parentId的等於頁ID我是在如果兒童的父對象

這對是我想做的事:

public IEnumerable<Route> GetAll(int locationId) 
{ 
    _db.Configuration.ProxyCreationEnabled = false; 
    return _db.Routes.Include(o => o.ConnectionPointRoutes.Select(s => s.Segment)) 
      .Include(o => o.ConnectionPointRoutes.Select(c => c.ConnectionPoint) 
      .Where(c => c.Location.LocationId == locationId)).ToList(); 
} 

,但我不斷收到這個錯誤:

類型「System.ArgumentException」的一個例外EntityFramework.dll發生,但在用戶代碼中沒有處理

附加信息:包含路徑表達式必須引用在該類型上定義的 導航屬性。對於 參考導航屬性使用虛線路徑,對於集合 導航屬性使用Select運算符。

有什麼想法?

+1

它看起來像c.Location是一個列表對象,所以你需要c.Location.Select(X => ......)。您是否擁有像偉大家長身份證,父母身份證,家長身份證,兒童身份證等多代表?如果是這樣,您可能需要使用遞歸算法來獲取所有級別。 Linq只能在單代列表上工作。 – jdweng

+1

我不認爲'Include'裏面支持Where'。 –

+0

我找到了修復它的方法,如果您感興趣,請檢查答案:) –

回答

0

我落得這樣做:

public IEnumerable<Route> GetAll(int locationId) 
    { 
     return _db.Routes 
      .Include(o => o.ConnectionPointRoutes.Select(s => s.Segment)) 
      .Include(o => o.ConnectionPointRoutes.Select(c => c.ConnectionPoint)) 
      .Where(c => c.ConnectionPointRoutes.Select(s => s.ConnectionPoint.Location) 
      .FirstOrDefault(q => q.LocationId == locationId).LocationId == locationId) 
      .ToList(); 
    }