Answer the question
In order to leave comments, you need to log in
How to display data from two related tables?
Greetings.
The figure shows a piece of the database schema. We are interested in tables: divisions, ranks, employees. In the DataGridView, I want the Rank and Division columns to have corresponding records from related tables instead of indexes. For this I did this:
private void СотрудникиФорма_Load(object sender, EventArgs e)
{
_context = new DatabaseSQLEntities();
_context.Configuration.LazyLoadingEnabled = true;
_context.Configuration.ValidateOnSaveEnabled = true;
var employeeList = (from employee in _context.Сотрудники
join rank in _context.Звания on employee.Звание equals rank.Код
join department in _context.Подразделения on employee.Подразделение equals department.Код
select new
{
employee.Имя,
employee.Фамилия,
employee.Отчество,
employee.Объем_груди,
employee.Рост,
employee.Пол,
rank.Звание,
department.Наименование
}).ToList();
сотрудникиBindingSource.DataSource = employeeList;
}
Answer the question
In order to leave comments, you need to log in
It was a test of attentiveness :) In the Subdivisions
table , change the Name column to Subdivision . The code, accordingly, will change to this:
public СотрудникиФорма()
{
InitializeComponent();
_context = new Database21SQLEntities();
_context.Сотрудники.Load();
var employeeList = (from employee in _context.Сотрудники
join rank in _context.Звания on employee.Звание equals rank.Код
join department in _context.Подразделения on employee.Подразделение equals department.Код
select new
{
employee.Имя,
employee.Фамилия,
employee.Отчество,
employee.Объем_груди,
employee.Рост,
employee.Пол,
rank.Звание,
department.Подразделение
}).ToList();
сотрудникиBindingSource.DataSource = employeeList;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question