Search code examples
c#asp.net-mvclinqentity-framework

Linq select in list with 2 many-to-many relationships


I have the following database structure:

USER <--> [user_client] <--> CLIENT <--> [client_application] <--> APPLICATION

USER, CLIENT and APPLICATION tables contain unique keys. user_client and client_application are many-to-many tables to map USERs to CLIENTs and CLIENTs to APPs.

I am using MVC5 / C#. The many-to-many tables are hidden in my Model by the Entity Framework.

What I want to achieve is the following: for a given USER, which has a list of CLIENTs, get the combined list of the distinct APPLICATIONs that all his CLIENTs have.

Could you please help with the logic and the Linq query (preferably in fluent syntax if possible)? Is it possible to do it in a single query without looping through the list of clients?

Thank you in advance.

Reda


Solution

  • Not sure it matches your schema but what about

    user.clients.SelectMany(c => c.applications).Distinct()