通过主键从外表获取数据

时间:2018-10-11 18:23:30

标签: c# winforms entity-framework combobox

在此设置中,我有一个所有者,他有10所学校。我在一个组合框中获得了所有者列表,并使用combox.selectedvalue选择具有ID的所有者,并使用其文本值来命名。现在,我有了一个数据网格视图,在其中要列出所有属于选定所有者的学校,并在数据网格视图的“所有者”表中列出所有者名称的附加列。

我想使用实体框架做到这一点。

Two Tables with Owner as primary key table and School as a Foreign key

Form

我在这里看到了很多问题,但是我没有找到我真正需要的东西。

1 个答案:

答案 0 :(得分:0)

取决于数据库的架构

在事件ComboBoxOwner_SelectedIndexChanged()

中尝试此操作
var selectedOwner = ((Owner)ComboBoxOwner.SelectedItem).Id;


GridViewSchools.DataSource = (from school in DbContext.Schools
                              join owner in DbContext.Owners on school.OwnerId equals owner.Id
                              where school.OwnerId == selectedOwner
                              select new { schoolName= school.Name , OwnerName = owner.Name }).ToList();

您可以通过相同的方法添加学校的所有信息

enter image description here

相关问题