LINQ: Internal join to the first row in a subquery?

Can anyone help?

I have 2 classes, basically 1 contains members and other sessions.

They are connected together with a common field called "name"

There is 1 member, but there may be MANY Sessions.

So, if I make a standard connection, I return to one member and several sessions. I just want to return the 1st row of sessions.

there is a SessioEndTime field in the session. So I need to order DESC for this to select the first record.

This is my linq, I have too many returns, I think I need a subquery, but I'm a bit confused.

var sessions = from m in this.members join s in this.sessions on m.Name equals s.Name select new { MemberName = m.Name, SessionTime = s.SessioEndTime}; 

Help us help.

EDIT

To make it clear, imagine that I have 5 members, each participant has NUMERIC sessions. I just want to get my 5 members, but with only one session, this session is the LAST session that can be obtained in SessionEndTime.

I think I made it a little clearer, if I don’t, then please let me know.

+4
source share
1 answer

Try the following:

 var sessions = from m in this.members join s in ( from se in this.sessions group se by se.Name into g select new {Name = g.Key, SessioEndTime = g.Max(a=>a.SessioEndTime)} ) on m.Name equals s.Name select new { MemberName = m.Name, SessionTime = s.SessioEndTime} 
+7
source

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


All Articles