Search code examples
c#entity-frameworkmany-to-manyinner-join

Inner join in Entity Framework


I have been having a problem with the following query I constructed it keeps returning null, I am hoping that someone could just point me in the right direction.

The query is meant to return a list of branches that offer a particular service based on a service ID that is given. I have a many-to-many relationship between two tables being branches and services.

from b in database.branches
join bs in database.branch_services on b.branch_id equals bs.branch_id
where bs.service_id == objID
select b;

Solution

  • Here is the valid query, adjust table names to match yours:

    database.Services.Where(s => s.ServiceId == 3).First().Branches.ToList();