To select from multipletables and map them to an object, you can use the Join and the Select operator. The
Join operator join the tables and the Select operator associates the rows of the joined tables to an object.
The join operator has the same syntax as previously described. The syntax for the select operator is:
select new { column1, column2 }
where column1 and column2 are the names of the columns that you want to map to the object.
For example, the following code joins the tables Customers and
Orders and maps the rows to a single object named CustomerOrder:
from customer in Customers
join order in Orders on customer.Id equals order.CustomerId
select new CustomerOrder { customer.Name, order.OrderId }
This code returns a new table that containing the names of all the customers and the IDs for all the orders. Each row in the new table will be an object of the
CustomerOrder type.
You can also use the Select operator to map the rows of the joined tables to another object. For example, the following code joins the
Customers, Orders, and Products tables and maps the rows to a single object called
CustomerOrderProduct:
from customer in Customers
join order in Orders on customer.Id equals order.CustomerId
join product in Products on order.ProductId equals product.Id
select new CustomerOrderProduct { customer.Name, order.OrderId, product.Name }
This code returns a new table containing the names of all the customers, their order IDs, and the names of all the products they ordered. Each row of the table is an object of the
CustomerOrderProduct type.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To select from multiple tables and map them to an object, you can use the Join and the Select operator. The
Joinoperator join the tables and theSelectoperator associates the rows of the joined tables to an object.The join operator has the same syntax as previously described. The syntax for the select operator is:
where
column1andcolumn2are the names of the columns that you want to map to the object.For example, the following code joins the tables
CustomersandOrdersand maps the rows to a single object namedCustomerOrder:This code returns a new table that containing the names of all the customers and the IDs for all the orders. Each row in the new table will be an object of the
CustomerOrdertype.You can also use the
Selectoperator to map the rows of the joined tables to another object. For example, the following code joins theCustomers,Orders, andProductstables and maps the rows to a single object calledCustomerOrderProduct:This code returns a new table containing the names of all the customers, their order IDs, and the names of all the products they ordered. Each row of the table is an object of the
CustomerOrderProducttype.