I am trying to write a slightly complex LINQ to SQL query.
I have a table called
Fruit (FruitID, FieldOne, FieldTwo)
and
FruitChangeHistory (FruitChangeHIstoryID, FruitID, Date)
What I want to do is return the Fruit list, to the view. But the View model will contain an extra field, LastChangeDate. So like: FruitID, FieldOne, FieldTwo, LastChangedDate
I need to work out how to join on the fruitchangehistory with the fruitid, then sort the dates and return only the latest change date.
This is what I have so far:
var list = from p in EntityFramework.Fruits
join h on EntityFramework.FruitChangHistory
on p.FruitID equals h.FruitID
orderby h.LastChangedDate ascending
select new FruitVM
{
FruitID = p.FruitId,
FieldOne = p.FieldOne,
FieldTwo = p.FieldTwo,
LastChangedDate = h.Date
}
but not quite working as planned.
Anyone can help?
Something like this: