How to sum values of children's children in Linq extensions

How to sum values of children's children in Linq extensions,linq,c#-4.0,entity-framework-5,Linq,C# 4.0,Entity Framework 5,I have a structure of related tables like so: Given the WorkReleaseDetail.WorkReleaseHeaderId, I want to get the aggregate sum of WorkOrderDetail.EstimatedQty grouped by WorkOrderDetail.ItemId using Linq extensions. Since I need to group the lowest table, I thought I'd be able to .Where like: WorkOrderDetail.WorkOrderHeader.WorkReleaseDetail.WorkReleaseHeaderId == id But no dice as you get stopped at the OrderGroupDetails as it is an aggregation, your only options past that are .Sum, etc,

I have a structure of related tables like so:

enter image description here

Given the WorkReleaseDetail.WorkReleaseHeaderId, I want to get the aggregate sum of WorkOrderDetail.EstimatedQty grouped by WorkOrderDetail.ItemId using Linq extensions.

Since I need to group the lowest table, I thought I'd be able to .Where like:

WorkOrderDetail.WorkOrderHeader.WorkReleaseDetail.WorkReleaseHeaderId == id

But no dice as you get stopped at the OrderGroupDetails as it is an aggregation, your only options past that are .Sum, etc, which makes sense.

Then I want to do this (which should work):

var woDtlRepos = new Repository<WorkOrderDetail>();
var workOrdDtl = woDtlRepos.SearchFor(
x => x.WorkOrderHeader.FacilityId == intFacilityId
&& x.WorkOrderHeader.WorkReleaseDetails.WorkOrderHeaderId == intWorkReleaseId);

var workRlsNeeds = workOrdDtl
.GroupBy(group => new
    {
        group.ItemId
    }
)
.Select(result => new 
    { 
        ItemId = result.Key.ItemId,
        TotalQty = result.Sum(a => a.EstimatedQuantity)
    }
);

How can I achieve this?


#1

Ahhh...had an epiphany... use .Any()

var workOrdDtl = woDtlRepos.SearchFor(
x => x.WorkOrderHeader.FacilityId == intFacilityId
&& x.WorkOrderHeader.WorkReleaseDetails.Any(woRlsHdr => woRlsHdr.WorkReleaseHeaderId ==      intWorkReleaseId));

#2

Ahhh...had an epiphany... use .Any()

var workOrdDtl = woDtlRepos.SearchFor(
x => x.WorkOrderHeader.FacilityId == intFacilityId
&& x.WorkOrderHeader.WorkReleaseDetails.Any(woRlsHdr => woRlsHdr.WorkReleaseHeaderId ==      intWorkReleaseId));