Reputation: 2643
I'm using Entity Framework and LINQ together in my project. I'm trying to fetch some data with following syntax,
I'm using following models, I have IList<TeamDetails>
public class OrganizationDetailsVM
{
public Guid Branch { get; set; }
public IList<TeamDetails> Teams { get; set; }
}
public class TeamDetails : Team
{
public IList<TeamMember> TeamMembers { get; set; }
}
The problem is that I'm trying to use following query to fill my OrganizationDetailsVM
but I can't actually reach my properties inside of my CounterDetails or TeamDetails class.
var teamQuery = _dbContext.PerfTeams
.Select(x => new OrganizationDetailsVM
{
Branch = filter.BranchId,
Teams = new List<TeamDetails>
{
//**I want my team details here as list of my TeamDetails class**
}
}).ToListAsync(cancellationToken);
As I mentioned in comments, here is related PerfTeamMembers classes.
public class TeamMember : BaseEntity
{
[Required]
[MaxLength(6)]
public decimal StaffId { get; set; }
public virtual Team Teams { get; set; }
}
Edit: PerfTeam class added, I've simplified my models for easily understanding.
public class Team : BaseEntity
{
[Required]
[StringLength(200)]
public string TeamName { get; set; }
[Required]
public Guid TeamTypeId { get; set; }
[Required]
[StringLength(4)]
public string TeamBranchId { get; set; }
[StringLength(4000)]
public string TeamDescription { get; set; }
[Required]
public Guid TeamPeriod { get; set; }
public virtual ICollection<TeamMember> TeamMembers { get; set; }
public virtual TeamType TeamTypes { get; set; }
}
Upvotes: 0
Views: 84
Reputation: 21353
According to your table structure, it seems that you want to get the Team and the related Team Members (since the TeamDetail just contains the TeamMember), in this scenario, you could try to use the following code:
var query = _dbContext.Teams
.Select(x => new OrganizationDetailsVM() {
Branch = x.TeamTypeId,
Teams = new List<TeamDetails>() {
new TeamDetails() {
TeamMembers = x.TeamMembers }
}
});
Besides, in the OrganizationDetailsVM class, you could also change the TeamDetail Property to TeamMember, then, when you query the Teams table, you could directly get the related TeamMembers. Code like this:
public class OrganizationDetailsVM
{
public Guid Branch { get; set; }
public List<TeamMember> TeamMembers { get; set; }
}
the query statement:
var query = _dbContext.Teams
.Select(x => new OrganizationDetailsVM()
{
Branch = x.TeamTypeId,
TeamMembers = x.TeamMembers.ToList()
});
Upvotes: 1
Reputation: 461
var teamQuery = _dbContext.PerfTeams.Include(p=>p.CounterMember).Include(p=>p.TeamMember). .Select(x => new OrganizationDetailsVM
{
Branch = filter.BranchId,
Counters = new List<CounterMember>(p.CounterMembers);//or whatever you called it in ef
{
//**I want my counter details here as list of my CounterDetails class**
},
Upvotes: 0