Entity structure gets the latest entries in the list of entries with the date

I have a sql server table with records (this is not new), it has 1 or more records (dimensions) for a person, and all records have a date and time field. So, for person 1, I have 5 records / measurements dated Jan 1, Jan 2, Feb 8, March 19 and July 2. I can have 10 entries for person 2, and the dates are also different.

Now I want to select the most recent entry for person 1 and person 2; to record both persons 1 with the new date and time value in the date and time field.

Can this be done using EF?

Regards, Michelle

data examples

personId Date Length

1 01-13-2009 180

1 01-14-2009 190

1 01-15-2009 184

2 02-18-2009 170

2 01-17-2009 190

In this table, I need the last entry for person 1 (= line 3) and for person 2 (= line 4)

+3
1
from p in Context.WhateverYourTableNameIs
group p by p.PersonId into g
select new 
{
    PersonId = g.Key
    MostRecentMeasurementDate = (DateTime?)
                                (from m in g
                                 order by m.Date desc
                                 select m.Date).FirstOrDefault())
}
+6

Source: https://habr.com/ru/post/1719292/


All Articles