0

In entity framework i have two table (two entities): People and Role with one to many relationship. In the People tables i have a navigation property to Role:

public virtual ICollection<Role> Roles { get; set; }

I'm able to retrieve all people that have role as 'barman':

var listPeople = (from p in SiContext.People
                  where p.Roles.Any(x => x.Name == "barman")
                  select p).ToList();

How can i retrieve all people with the related role? I want the person name and surname and the role name (that is in the role table)

Thanks

2 Answers 2

2

This?

var peoples = SiContext.People.Include("Roles");

or

var peoples = SiContext.People.Include(p => p.Roles);
Sign up to request clarification or add additional context in comments.

Comments

0

You can select from multiple entities:

from p in SiContext.People
from r in p.Roles
select new { Name = p.Name, ..., Role = r.Name }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.