I have a question similar to this entry:
how-do-i-query-sql-for-a-latest-record-date-for-each-user
... but I need it in Linq.
In short for the people who do not care to read the other link:
I have a history table with devices. I need the latest entry for each device before a given date. So I do not need only a specific value, but the whole row.
Example data:
ID | DeviceId | State | LastUpdatedDate
0 | 1 | Online | 2016-01-05 10:23:45
1 | 2 | Offline | 2016-01-04 00:05:33
2 | 1 | Offline | 2016-01-01 06:13:25
3 | 1 | Online | 2016-01-07 11:02:06
4 | 3 | Offline | 2016-01-03 18:00:25
5 | 4 | Online | 2016-01-08 03:00:05
4 | 3 | Offline | 2016-01-08 04:27:21
So, if I would like to have the last known states before 2016-01-05, I would expect the following result:
ID | DeviceId | State | LastUpdatedDate
1 | 2 | Offline | 2016-01-04 00:05:33
2 | 1 | Offline | 2016-01-01 06:13:25
4 | 3 | Offline | 2016-01-03 18:00:25
NULL | 4 | NULL | NULL
The NULL entry is not required, I just added it for clarity. Again, I would need this in Linq. I could easily do this with a foreach in code, but I do not want to make 700 calls to the database. I would like this to be fairly efficient.
Thanks!