Linq To Sql - Getting The Last Know History Entry For Each Divice, Prior To A Certain Date
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 ot
Solution 1:
Something like this should work:
DateTime date = //... (e.g. 2016-01-05)
var result =
entries //e.g., context.DeviceHistoryEntries
.GroupBy(x => x.DeviceId)
.Select(gr =>
gr
.Where(x => x.LastUpdatedDate < date)
.OrderByDescending(x => x.LastUpdatedDate)
.FirstOrDefault()) //This will give us null for devices that
//don't have a status entry before the date
.AsEnumerable()
.Where(x => x != null) //remove null values (optional)
.ToList();
Post a Comment for "Linq To Sql - Getting The Last Know History Entry For Each Divice, Prior To A Certain Date"