I am trying to find the most efficient way to get the most recent record in a joined table in LINQ.
This query may handle thousands of records so I didn’t want to perform a subquery.
I need everything from items, but only the most recent date from the “Notes” table, whose field name is SubmittedDate.
var items = (from t1 in db.Items
from t2
in db.Notes
.Where(o => (t1.Item_ID == o.Item_ID))
select new ItemModel
{
Name = t1.Name,
MostRecentUpdate = t2.SubmittedDate <== Need max value in model
});
Any help would be greatly appreciated.
It looks like you probably just want a group join:
Now
MostRecentUpdateshould be null if there are no non-null dates in the matchingNotesrows. At least, that’s what the LINQ to Objects behaviour would be, so fingers crossed the abstraction holds…