Using the sample query in the documentation under the title "Hierarchical sets", I constructed the following query...
var result = from wo in metaData.WorkOrder
join proj1 in metaData.Project on wo.ProjectId equals proj1.ProjectId
where wo.WorkOrderId == workOrderId
select new
{
WorkOrderId = wo.WorkOrderId,
LoanNumber = wo.LoanNumber,
BorrowerName = wo.BorrowerName,
ClientReferenceNumber = wo.ClientReference,
DueDate = wo.DueDate,
EnteredDate = wo.EnteredDate,
StatusLastUpdated = wo.StatusLastUpdated,
StatusId = wo.StatusId,
Project = from proj in metaData.Project
where proj.ProjectId == wo.ProjectId
select new
{
ProjectId = proj.ProjectId,
ConfirmationNumber = proj.ConfirmationNbr,
ReceivedDate = proj.ReceivedDate,
ProjectName = proj.ProjectName
},
Product = from negProd in metaData.NegotiatedProduct
where negProd.NpId == proj1.NpId
select new
{
DisplayText = negProd.DisplayText,
ProductName = negProd.NpName
}
};
In the anonymous type that is being built in the code sample above, the members "Project" and "Product" are of type IQueryable.
Question1: When are these queries executed? I am guessing they dont execute until I call First(), FirstOrDefault() etc.
Question 2: In order to access the values / entities associated with these members do I need to write the code below, or is there another way, aside from "prefetches"?
foreach (var item in result)
{
var project = item.Project.FirstOrDefault();
var product = item.Product.FirstOrDefault();
return new WorkOrderHeader
{
ProjectId = project.ProjectId,
ConfirmationNumber = project.ConfirmationNumber,
WorkOrderId = item.WorkOrderId,
ProductName = product.ProductName
};
}
Question 3: If I want to project into an object using the "Hierarchial Sets" method that we are talking about here, am I required to project into an "Anonymous Type" or can I project into a known type? i.e. can I project directly into WorkOrderHeader?