1

I seem to have a big misunderstanding of how EF Core / Linq / Navigation Properties work.

I tried to extend my example from a previous question adding a m:n relationship.

Database tables:

  1. Person {Id (in), Firstname (nvarchar), Lastname (nvarchar) }
  2. Group {Id (int), Name (string) }
  3. GroupAssignment {Id (int), PersonId (int), GroupId (int) }

Database data: The person with Id 1 is assigned to the groups 1 and 3.

My query returns as expected the linked GroupAssignments:


var result = from person in _dbContext.Person
            select new
            {
                id = person.Id,
                firstname = person.Firstname,
                lastname = person.Lastname,
                groupAssignments = person.GroupAssignment  
            };

return Ok(result);

But I want to get a list with the fields of the N table (Groups). The result I am looking for is


[
{
    "id": 1,
    "firstname": "First1",
    "lastname": "Last1",
    "groupAssignments": 
    [
         {
         "id": 1,
         "name": "test group 1"
         },
         {
         "id": 3,
         "name": "test group 3"
        }
    ]
}
]

By the way: I would be really happy if you post some good reading links about EF (core) and linq into the comments. It seems I have a lot of beginner problems.

Community
  • 1
  • 1
monty
  • 7,888
  • 16
  • 63
  • 100

1 Answers1

2

You might have a Group navigation property in GroupAssigment entity . If that is the case use Select extension method:

var result = from person in _dbContext.Person
            select new
            {
                id = person.Id,
                firstname = person.Firstname,
                lastname = person.Lastname,
                groupAssignments= person.GroupAssignment.Select(ga=>ga.Group)  
            };

return Ok(result);

About documentation, you can start here.

Update

To achieve what you commented below you can use an anonymous type to project only those two properties:

groupAssignments= person.GroupAssignment.Select(ga=>new{id=ga.Group.Id,name=ga.Group.Name}) 
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • Thanks for the link to the documentation. This one I knew but unfortunatly it doesn't cover simple stuff like that from the question. It's acutally to less information to get started beyond 101. m:n would be 102 I guess. Your code works great, big thanks but would you mind adding a second code snippet on how to get rid of the "groupAssignment": [] so just pick id and name of the group? – monty Oct 11 '16 at 14:16
  • 1
    Hi, @monty, take a look my update and let me know if that is what you looking for – ocuenca Oct 11 '16 at 14:22
  • Thank you again. That was exactly what I was looking for. Saved my day an a couple of the following ones. – monty Oct 11 '16 at 14:26
  • Sorry for the late detail but where would I place a group by for the ga.Group.Id to get only one entry if the Person is (for some reason) in the same group twice? – monty Oct 13 '16 at 05:57
  • Figured it out :-) – monty Oct 13 '16 at 12:17