views:

55

answers:

2

Hi all,

i have a sql server table with records (that isn't new) it has 1 or more records (measurements) for a person, and all records have a datetime field. So for person 1 i have 5 records / measurements, dated jan 1, jan 2, feb 8, march 19 and july 2. i have maybe 10 records for person 2, and the dates are different too.

Now i want to select for both person 1 and person 2 the latest record; for both persons 1 record, with the newewst datetime in the datetime field.

Can that be done with the EF?

Kind regards, Michel

example data

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 would like the latest record for person 1 (= row 3) and for person 2 (=row 4)

A: 

this can be done, but we may need more information to help you out. for example a scema or table structure. table columnd names, table name...

Alexandre Brisebois
i've edited the question
Michel
+1  A: 
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())
}
Craig Stuntz
hm, i have not a Measurement table.
Michel
Obviously I guessed at the table name since you didn't post it in your question.
Craig Stuntz
sorry, my comment was a little short, what i meant to say whas that i don't have a measurement collection in my person object, but that the measurement data and the person id are all in one table, there is no person table but only the measurements table.
Michel
See updated reply
Craig Stuntz
That did the trick. weird object type, the 'g', at first sight. it has a key and all the records in it. But: learn it, love it, use it.
Michel