Reputation: 67
I'm probably being an idiot and I've had been searching around for the past 2 hours looking for an answer to this, the issue is likely to be that I'm unsure on how to phrase it.
Basically I'm new to code first and using it in this project and unsure on how to query it.
I've got three classes
Profile > Teams > Events
I need to get a list of the events which a specific user can access, the user must be a member of a the team that owns said event.
A user (profile) can be a member of many teams and I need a list of events from all teams.
I've chopped down my classes, as most of it is irrelevant to what I'm asking currently
private void BindEvents()
{
var db = new Context();
var events = db.Profiles
.Include("Teams")
.Include("Teams.Events")
.Where(p => p.User_ID == user)
.FirstOrDefault().Teams.Select(t => new { t.Events }.Events);
gvUpcomingEvents.DataSource = events;
gvUpcomingEvents.DataBind();
db.Dispose();
}
public class Profile
{
[Key]
public Guid User_ID { get; set; }
[Required, MaxLength(10)]
public string Title { get; set; }
[Required, MaxLength(50)]
public string Forename { get; set; }
[Required, MaxLength(50)]
public string Surname { get; set; }
public ICollection<Team> Teams { get; set; }
public Profile()
{
Teams = new HashSet<Team>();
}
}
public class Team
{
[Key]
public int ID { get; set; }
[Required, MaxLength(150)]
public string Name { get; set; }
[Required]
public bool Active { get; set; }
//Foreign Keys
public ICollection<Profile> Users { get; set; }
public ICollection<Event> Events { get; set; }
public Team()
{
Users = new HashSet<Profile>();
}
}
public class Event
{
[Key]
[Required]
public int ID { get; set; }
[Required]
public Guid GUID { get; set; }
}
Upvotes: 0
Views: 85
Reputation: 9604
try SelectMany
var events = db.Profiles
.Where(p => p.User_ID == user)
.SelectMany(p=>p.Teams.SelectMany(t=>t.Events));
Upvotes: 1