I would like to create this using the Entity Framework. A list of new objects containing fields from the parent and fields from the current child record. I would write SQL as a correlated subquery:
SELECT p.PolicyNumber, p.HomeState, pt.RevisionDate, pt.TranStatus FROM dbo.Policy p JOIN dbo.PolicyTran pt ON p.Id = pt.Policy_Id AND pt.RevisionDate = ( SELECT MAX(mpt.RevisionDate) FROM dbo.PolicyTran mpt WHERE p.Id = pt.Policy_Id ) WHERE p.HomeState = 'NY'
The policy context has navigation to the list of transactions (PolicyTran).
var query = context.Policies.Include(t => t.PolicyTransactions);
No matter what I try to do, Linq is incorrect or SQL is incorrect. Time to call specialists.
source share