3

Using the include method does not include the children of a collection.

var abuseCaseQuery =
  from @event in 
    context.AbuseEvents.Include("AbuseCase.AbuseCaseStatus.Status")
  select new
  {
    @event.SecurityGroupId,
    @event.AbuseCaseId,
    @event.AbuseCase.AbuseCaseStatus
  }
;

var abuseCases = abuseCaseQuery.ToList();

The abuseCases List contains all the AbuseCasesStatus and the StatusId but the Status object is null.

edmx:

edmx

How can I populate the Status navigation property?

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
Tim Blackwell
  • 699
  • 1
  • 7
  • 18

1 Answers1

1

In EF, Includes are ignored when you add a projection with Select.

So, you have to explicitly select all the data you want:

select new
{
  @event.SecurityGroupId,
  @event.AbuseCaseId,
  @event.AbuseCase.AbuseCaseStatuses
    .Select( acs => new { AbuseCaseStatus = acs, Status = acs.Status } )
}

This does result in a "flat" object, so you might like to fix up the object graph afterwards:

foreach( var abuseCase in abuseCases )
{
  foreach( var acs in abuseCase.AbuseCaseStatuses )
  {
    acs.AbuseCaseStatus.Status = acs.Status;
  }
}
Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • I was unable to select the collection @event.AbuseCase.AbuseCaseStatus.Status. I am trying to use the select many function but having trouble with it. – Tim Blackwell Apr 02 '13 at 11:59
  • The `AbuseCaseStatus` table has a `StatusId` column, so its `Status` navigation property should be a single `Status` object - is this not the case? – Nick Butler Apr 02 '13 at 12:39
  • Sorry, I was unclear. Yes an `AbuseCaseStatus` object has one `Status` but in my select, `@event.AbuseCase.AbuseCaseStatus` is a collection of all the `AbuseCaseStatus` for that `AbuseCase` – Tim Blackwell Apr 02 '13 at 13:14
  • My fault - I didn't look at your model closely enough. I've updated my answer. – Nick Butler Apr 02 '13 at 13:25