I have a puzzling problem I am trying to solve. I have a list of users (Identities table) that is related through a foreign key to a Sessions table (1 to 0/many)
What I am trying to do is find the most recent closed session for a user. Normally I figure out the SQL first, then try to translate into LINQ. I am FAR from an expert in LINQ, and my latest query is giving me fits.
Here is the SQL I am working with.
IdentityId is the PK in the Identities table - the FK to the Sessions table
select s.* from Identities i
join Session s on s.IdentityID = i.IdentityID
where s.ID in
(select top 1 ID from Session a
where a.IdentityID = s.IdentityID
order by a.CreateDate desc
)
This gives me exactly what I want; 1 session row per identity row AND, it is the most recent session (CreateDate is the most recent)