Search code examples
c#sqlentity-frameworkrdbms

EF - pure join table with three or more fields


at this time i know how to do PJT with two fields for M:N relationships, but i want to know if is possible to do with three or more fields for several M:N relations. I have an example:

Three tables, activities, users, clients. I want to know if is possible, only with navigation properties of the entities do something like: Find activities of one client perfomed by one user:

ActivitySet selectedActivities = ClientSet
  .First(c => c.Id == enteredId)
  .Activities.Find(a => a.UserId == enteredUserId);

Or any combination possible.


Solution

  • It's possible, but you need to go in the opposite direction, from the entity you want, back up.

    Activities
      .Where(a => a.Client.Id == enteredId)
      .Where(a => a.User.Id == enteredUserId)