I have the following classes:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public DateTime JoinedCompany { get; set; }
public ICollection<EmployeeSkill> EmployeesSkills { get; set; }
}
public class Skill
{
public int Id { get; set; }
public string SkillName { get; set; }
}
public class EmployeeSkill
{
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
public int SkillId { get; set; }
public Skill Skill { get; set; }
}
I also have a DbContext class:
public DbSet<Employee> Employees { get; set; }
public DbSet<Skill> Skills { get; set; }
public DbSet<EmployeesSkill> EmployeeSkills { get; set; }
Now, I can get back the data with the following:
public async Task<IReadOnlyList<Employee>> GetEmployeesAsync ()
{
return await _context.Employees
.Include (p => p.EmployeeSkills)
.Include (p => p.Skill)
.ToListAsync ();
}
which returns something like this:
[
{
"firstName": "Olivia",
"lastName": "Lu",
"gender": "F",
"joinedCompany": "2015-03-31T00:00:00",
"employeeSkills": [
{
"employeeId": 8,
"skillId": 3,
"skill": {
"skillName": "Hadoop",
"id": 3
},
"id": 1
},
{
"employeeId": 8,
"skillId": 4,
"skill": {
"skillName": "Spark",
"id": 4
},
"id": 2
}
],
"id": 8
}
]
I get really tripped up when using LINQ and I can never manage to find the right solution. I am trying to end up with the following:
[
{
"firstName": "Olivia",
"lastName": "Lu",
"gender": "F",
"joinedCompany": "2015-03-31T00:00:00",
"employeeSkills": [
{
"skillId": 3,
"skillName": "Hadoop"
},
{
"skillId": 4,
"skillName": "Spark"
}
],
"id": 8
}
]
How can I change the LINQ query as to get a result as per above?
Thanks for your help.