Context: I have a parent table (Product), a child table (Transactions), and a child data table (RelatedData).
A Product has many transactions. Transactions of type 'A' have a RelatedData entry with SpecialData.
I would like to get all Product records that have their LAST transaction of type 'A' with the SpecialData value of 'B'
I'm using linq to SQL on a MSSQL database.
var data = from p in db.Product
select p;
//various filters
data = data.Where(...);
What I have right now for the query I want to implement:
data = data.Where(p=> p.Transactions
.Where(t => p.ProductID == t.ProductID &&
t.TransType == 'A')
.OrderByDescending(t => t.DateTime)
.FirstOrDefault(t => t.RelatedData //Get latest
.Single().SpecialData == 'B')
!= null
);
It isn't returning any of the test data I've got set up in the database. Please help.