In this guide you will see how to use linq for a join operation on your data in Entity Framework
The JOIN statement is usually used to associate elements from different sources, when they share some value that can be compared for equality.
And when you don’t have navigation properties.
Let’s see how to do this, using Linq.
Contents for Entity Framework: Linq Join Example C#
Starting Point
We created a really simple database created in the Entity Framework Database First Approach tutorial, and we will use it.
Linq Join
Using Linq, we don’t have to write any SQL code:
.Join(
context.RobotFactories,
d => d.RobotFactoryId,
f => f.RobotFactoryId,
(d, f) => d)
.ToList();
As you can see, we used the Join method with the dot notation.
Join Structure
context.RobotDogs This is the starting point, the “from” statement
.Join(context.RobotFactories, The table to join with
d => d.RobotFactoryId, The primary key
f => f.RobotFactoryId, The foreign key
(d, f) => d) What to select from the join