Search⌘ K

Challenge Solution: Query Data

Explore how to query data in Entity Framework Core by solving practical tasks involving instructors, students, and their departments. Learn to use LINQ, eager loading, joins, skip method for pagination, and anonymous objects to fetch and display related data effectively.

Overview

Let’s solve the tasks with the project below.

{
    "version": "0.2.0",
    "configurations": [
        {
            // Use IntelliSense to find out which attributes exist for C# debugging
            // Use hover for the description of the existing attributes
            // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net6.0/QueryData.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}
Solution for querying from the school data

Click the “Run” button above, then execute the command below in the terminal:

C#
dotnet run

A snippet from the output is below:

Shell
LASTNAME FIRSTNAME DEPARTMENT-TITLE
Olu Abraham Engineering
Umoh Aniekan Medicine
---STUDENTS---
DEPARTMENTS STUDENTS-FULLNAME
Engineering Rock Prince
Medicine Crystal Susan
Medicine Ayorinde Femi
Engineering Rawlins Bob
Medicine Kipp Johanesson
Engineering Okon Blessing

Solution breakdown

Let’s review the tasks required in the challenge.

Fetch and display instructors and their departments

Note the following in Program.cs of the project: ...