G
G
georgich2015-10-07 09:56:15
C++ / C#
georgich, 2015-10-07 09:56:15

How to display data from two related tables?

Greetings.
513254ee359a4ab686c0f2d796a6148f.png
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;
        }

The rank is displayed, but the Division does not want to. What am I doing wrong?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
georgich, 2015-10-07
@georgich

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;
        }

PS Unfortunately, there are fewer and fewer specialists on Habré and on the Toaster...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question