0
我不知道我缺少这一点: 我有我的EDM-这种关系(不能加载图片还)WPF树视图使用LINQ to实体多重关系
Category_1_ _ 许多 RecipeCategory_ 许多 _ _1_Recipe
基本上每个配方都有一个或多个类别,每个类别可以有0个或多个配方
这里是我的代码:
private void LoadData()
{
List<Category> Categories = new List<Category>();
List<Recipe> Recipes = new List<Recipe>();
var ctx = new MaWEntities();
var query = from x in ctx.Categories select x;
Categories = query.ToList<Category>();
foreach (Category c in Categories)
{
TreeViewItem TVP = new TreeViewItem() { Header = c.CategoryName.ToString() };
var qq = from xx in ctx.RecipeCategories.Where(o => o.CategoryId == c.CategoryId) select xx;
foreach (var rc in qq)
{
TreeViewItem TVC = new TreeViewItem() { Header = rc.Recipe.RecipeName.ToString() };
TVP.Items.Add(TVC);
//TVP.IsExpanded = true;
}
}
}
我想完成的是一个treeview,其父节点是categoryname,子节点是recipe名称。我知道如何使用Linq从基表(配方到RecipeCategory)钻入关系表中。还必须有一种方法再次遍历分类表。我还应该提到,即使该类别没有任何食谱,我仍然希望在树视图中将类别名称看作父级。