How to get entities from multiple tables with one LINQ call?
How to get entities from multiple tables with one LINQ call?
20305-Sep-2023
Updated on 06-Sep-2023
Home / DeveloperSection / Forums / How to get entities from multiple tables with one LINQ call?
How to get entities from multiple tables with one LINQ call?
Aryan Kumar
06-Sep-2023You can get entities from multiple tables with a single LINQ call using the
Include
operator. You can load related entities from another table into the current entity using the include operator.The syntax for
Include
operator is as follows:where
entity
is the name of the entity loading the related entitiy andrelatedEntity
is the name of the related entity.For example, the following code retrieves all the customers and their orders in a single LINQ call:
This code will first load all the customers from the
Customers
table. Then load all the orders for each customer from theOrders
table.You can also load multiple related entities in a single LINQ call using the include operator. For example, the following code gets all the customers, their orders, and the products that they ordered in a single LINQ call:
This code will first loads all the customers from the
Customers
table. Then, load all the orders of each customer from theOrders
table. Finally, all the products ordered by each customer are loaded from the Products table.