I have the following POCOs:
public class TrackToken
{
[Required, Key]
public int ID { get; set; }
[MaxLength( 500 )]
public string Comment { get; set; }
[Required]
public DateTime CreationTime { get; set; }
public virtual ICollection<TrackLog> Logs { get; set; }
}
public class TrackLog
{
[Required, Key]
public int ID { get; set; }
[Required]
public TrackToken Parent { get; set; }
[Required]
public DateTime Time { get; set; }
[Required]
public int ServerID { get; set; }
}
I'd like to be able to make a EF LINQ query that can return every TrackToken, and join with the most recent TrackLog for that TrackToken.
I've tried code like:
TrackTokens
.Select( t => new
{
ID = t.ID,
Comment = t.Comment,
CreationTime = t.CreationTime,
RecentLog = t.Logs.OrderByDescending( l => l.Time ).FirstOrDefault(),
} );
But somewhere along the line of executing the query, I get
EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details.
InnerException: NotSupportedException: Specified method is not supported.
Which appears to be related to the usage of FirstOrDefault.
I've also tried something similar to this, but I'm unsure of how to get the ServerID column
TrackTokens
.Select( t => new
{
ID = t.ID,
Comment = t.Comment,
CreationTime = t.CreationTime,
LastSeen = t.Logs.Max( l => l.Time ),
LastServerID = // how do I get the ServerID column of the most recent record?
} );
What's the go-to method of performing a query on this sort of relationship?