Let’s say I’ve got a table called [Items] which has a primary key ItemID, and I’ve got a table called [ItemStatuses] which is linked with the ItemID, has a Auto-Increment ID and has a Date (and other columns to capture various things)
Which means, for every Item, I’ll have many ItemStatuses.
Items:
ItemID
1
2
ItemStatuses:
ID ItemID Date
----------------------------
1 1 1/1/2010...
2 1 1/2/2010...
3 1 1/3/2010...
4 2 1/1/2010...
5 2 1/2/2010...
I want to be able to join each Item with the latest ItemStatus. I’ve got a solution which grabs the information into entity objects and then does the work, but this takes so much time. If I could query this using LINQ2SQL it would be so much faster. I’ve read several other questions on stackoverflow but none of the examples make sense for my scenario. I’ve tried to figure this out, but still cannot get it to work.
I need this:
ItemID Date
-----------------------
1 1/3/2010...
2 1/2/2010...
This is a simplistic example. I don’t want just specific columns. There are MANY columns in the Items and ItemStatuses that I need to capture. But the ones mentioned are the control columns.
If someone could give me a hand I would really appreciate it.
The query I have now doesn’t work, but it’s where I’m at right now:
var results = from i in context.Items
from s in context.ItemStatuses
where s.ItemID.Equals(i.ItemID) s.Date <= inputDate
orderby s.Date descending
select new { i, s };
The Following query joins the two tables and selects only the
ItemStatusesthat has the latestDatevalue: