Sobes.tech
Junior — Middle

How to implement table joins when working with Entity Framework?

sobes.tech AI

Answer from AI

In Entity Framework, navigation properties and LINQ queries are typically used for joining tables.

For example, if there are two entities Order and Customer, related by a foreign key CustomerId, you can retrieve orders with customer information like this:

var ordersWithCustomers = context.Orders
    .Include(o => o.Customer) // eager loading of related data
    .Where(o => o.Amount > 100)
    .ToList();

If you need to perform an explicit join, you can use LINQ JOIN:

var query = from o in context.Orders
            join c in context.Customers on o.CustomerId equals c.Id
            select new { o.Id, o.Amount, CustomerName = c.Name };

var result = query.ToList();

Thus, Entity Framework allows working with table joins through navigation properties and LINQ queries, simplifying data handling.