2014-02-17 70 views
0

我有以下DB結構。 enter image description hereMVC EF選擇多個連接

我想使用實體框架進行選擇。我希望我的所有Categories可能有一個CategoryText。如果他們有CategoryText我也需要得到Language

我搜索,我無法找到任何有用的東西。 這裏是我的查詢不起作用:

var categoriesSQL = db.Categories 
        .Include(i => i.CategoryTexts.Select(s => s.Language) 
        .Where(w => w.Format == (string)Session["chosen_language"])); 
var categories = categoriesSQL.ToList(); 

它拋出: The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path

我嘗試了很多的方法和recomandations解決這個,但我無法找到一個解決方案。

我想只有一個查詢做出的選擇。

謝謝!

回答

1

試試這個

var language = (string)Session["chosen_language"]; 
var categoriesSQL = db.Categories 
        .Include(i => i.CategoryTexts.Select(s => s.Language)) 
        .Where(c => 
          (from ct in c.CategoryTexts 
          from l in ct.Languages 
          select l.Format).Contains(language) 
        ); 
var categories = categoriesSQL.ToList(); 

OR

var language = (string)Session["chosen_language"]; 
var categoriesSQL = db.Categories 
        .Include(i => i.CategoryTexts.Select(s => s.Language)) 
        .Where(c => c.CategoryText 
           .Any(ct => ct.Languages 
             .Any(l => l.Format == language) 
           ) 
        ); 
var categories = categoriesSQL.ToList();