我有以下数据库结构。
我想使用Entity Framework进行选择。我希望我的所有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
我试图通过很多方式解决这个问题并重新尝试,但我找不到解决办法。
我希望只在一个查询中进行选择。
谢谢!
答案 0 :(得分: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();