Skip to content

About

Explanation of .leftJoin() and .rightJoin() in EF Core 10+ LINQ. Code used in my blog post (https://www.thinkasadev.com/)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Left join & Right Join in EF Core

This is a sample project explained on https://thinkasadev.com that demonstrates the use of .leftJoin() and .RightJoin() in Entity Framework Core 10.

Project Startup

ℹ️ To run this project, you need a running instance of SQL Server.

  1. Start SQL Server via Docker

    You can start a SQL Server Docker container using the following commands. Be sure to replace <INSTANCE-NAME> with a strong password and <INSTANCE-NAME> with name of your db instance.

    Username is sa

   docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<PASSWORD>"  -p 1433:1433 --name <INSTANCE-NAME> --hostname <INSTANCE-NAME> -d mcr.microsoft.com/mssql/server:2025-latest
  1. Create appsettings.json

    Duplicate appsettings.template.json in appsettings.json and set your connectionstring.

  2. Apply Migrations to the Database

    If docker container is started and appsettings.json is ready, open the Package Manager Console in Visual Studio and run the following command to create the tables and apply the data seeding:

    Update-Database

Once these steps are completed, the application will be ready to connect to the database and interact with the data.

Learn

ℹ️ Go to my blog https://thinkasadev.com and read post about Left Join & Right Join in EF Core 10.

In Service/EmpireService.cs you can find different functions demonstrating left join and right join operations using EF Core 10, EF Core 9 or less and SQL Query.

LEFT JOIN structure:

    var query = 
        await _context.<TABLE_1>.LeftJoin(
            _context.<TABLE_2>,
            table1 => table1.<ID>,
            table2 => table2.<ID>,
            (table1, table2) => new
            {
                Field1 = table1.field1,
                Field2 = table2 != null ? table2.field1 : (int?)null
            })
        .ToListAsync();

RIGHT JOIN structure:

    var query = 
        await _context.<TABLE_1>.RightJoin(
            _context.<TABLE_2>,
            table1 => table1.<ID>,
            table2 => table2.<ID>,
            (table1, table2) => new
            {
                Field1 = table1.field1,
                Field2 = table2 != null ? table2.field1 : (int?)null
            })
        .ToListAsync();

About

Explanation of .leftJoin() and .rightJoin() in EF Core 10+ LINQ. Code used in my blog post (https://www.thinkasadev.com/)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages