locked
Include over a many-to-many relationship RRS feed

  • Question

  • Hi

    In my BD schema i have these three tables: Entreprise, EntrepriseUser and User.

    Entreprise:

    EntrepriseId

    ...

    EntrepriseUser:

    EntrepriseId

    UserId

    User:

    UserId

    ...

    EntrepriseUser is the many-to-many relationship table among Entreprise and User.

    I want to retrieve all the entreprises where UserId = X but including another entity (Owner) related with Entreprise (one owner multiple entreprises).

    First i got it with a join and without including Owner's, but now i want to include Owner's and Include does not work inside a join query. 

    How can i do this with an ObjectQuery (along with a include) ? 

    Thanks

    Pedro Governo

    Wednesday, August 22, 2012 7:35 PM

Answers

  • Hi Desbaratizador,

    Welcome to MSDN Forum.

    Please refer to the query below.

    var query = context.Enterprises.Include(e => e.Owner).Where(e => e.Users.Any(x => x.userId == 1));

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, August 23, 2012 1:39 AM

All replies

  • Hi Desbaratizador,

    Welcome to MSDN Forum.

    Please refer to the query below.

    var query = context.Enterprises.Include(e => e.Owner).Where(e => e.Users.Any(x => x.userId == 1));

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, August 23, 2012 1:39 AM
  • Hi

    Thanks, your example helped a lot but i think that should be:

    var query = context.Enterprises.Include(e => e.Owner).Where(e => e.EntreprisesUsers.Any(x => x.userId == 1));

    Desbaratizador

    Friday, August 24, 2012 1:54 PM