.net - How can I get the expected result (Attached Image) using in C# Entityframework?

Viewed 55

I have three Tables

Students

ID Name
1 A
2 B
3 C

Skills

ID Name
1 Java
2 Python
3 C++

StudentsSkill

ID StudentID SkillID
1 1 1
2 1 2
3 2 2
4 2 3
5 3 1
6 3 2
7 3 3

I'm getting Skills as null in result like below:

[

   {
    "id": 1,
    "name": "A",  
    "Skills": [],
  },

  {
    "id": 2,
    "name": "B",  
    "Skills": [],
    
  },

  {
    "id": 3,
    "name": "C",  
    "Skills": [],
    
  },

]

How can I get all Students Details along with Skills like output in the attached image:

Expected Output:
Expected Output

Consider studentList has all details of Student table and skillList has all details of Skill table,

Definition of classes

 public partial class Skill
    {
        public Skill()
        {
            StudentSkills= new HashSet<StudentSkill>();
        }

        public int Id { get; set; }
        public string Name { get; set; } = null!;

        public virtual ICollection<StudentSkill> StudentSkills{ get; set; }
    }


    public partial class Student

    {
        public Student()
        {
            StudentSkills= new HashSet<StudentSkill>();
        }

        public int Id { get; set; }
        public string Name{ get; set; } = null!;

        public virtual ICollection<StudentSkill> StudentSkills{ get; set; }

    }

        public partial class StudentSkill
     {
        public int Id { get; set; }
        public int StudentId { get; set; }
        public int Skill Id { get; set; }

        public virtual Student Student { get; set; } = null!;
        public virtual Skill Skill { get; set; } = null!;
}

How to write query to achieve expected output ?

0 Answers
Related