Return most recent DateTime C# Entity Framework Job -&gt; Many Quotes 
Quote -&gt; EmailLogList
EmailLogList -&gt; Many EmailLogs


I want to return a CreatedDate from an EmailLog which has been created most recently - using a Job.Guid.

Is this the best way possible?

public DateTime? GetMostRecentEmailLog(Guid jobGuid)
{
    using (DbContext dbContext = new DbContext())
    {
        IEnumerable&lt;Guid?&gt; logLists = dbContext
            .Jobs
            .Include(j =&gt; j.Quotes)
            .First(j =&gt; j.Guid == jobGuid)
            .Quotes
            .Where(q =&gt; q.EmailLogListGuid != null)             
            .Select(q =&gt; q.EmailLogListGuid);

        return dbContext
            .EmailLogs
            .OrderByDescending(e =&gt; e.CreatedDate)                  
            .Where(e =&gt; logLists.Contains(e.EmailLogListGuid))
            .Select(e =&gt; e.CreatedDate)
            .FirstOrDefault();                              
    }
}
This frequently used pattern...


entities.First().Property



... is hardly ever an economical way to do querying. It fetches a complete entity from the database of which subsequently all but one property is discarded. Or, when Property is a lazy loaded collection, everything is discarded.

In general a better way is...


entities.Where(e =&gt; e.Id == id).Select(e =&gt; e.Property).First()



... because this only gets the single property from the database.

Or, if Property is a collection,...


entities.Where(e =&gt; e.Id == id)
        .SelectMany(e =&gt; e.Property)
        .Select(p =&gt; p.Property1)



Applying this would turn the first statement into...

IEnumerable&lt;Guid&gt; logLists = dbContext.Jobs
                                      .Where(j =&gt; j.Guid == jobGuid)
                                      .SelectMany(j =&gt; j.Quotes)
                                      .Where(q =&gt; q.EmailLogListGuid != null)             
                                      .Select(q =&gt; q.EmailLogListGuid.Value);


The second part is OK. Getting the most recent item from a collection always requires some ordering that defines "most recent".

Note that using the improved form, EF will translate this into one query, because logList isn't materialized yet. It is an expression tree1 that can be merged with the second expression. If you want, you can create one statement in LINQ as well:

return dbContext.Jobs
                .Where(j =&gt; j.Guid == jobGuid)
                .SelectMany(j =&gt; j.Quotes)
                .SelectMany(q =&gt; dbContext.EmailLogs
                    .Where(e =&gt; q.EmailLogListGuid == e.EmailLogListGuid))
                .OrderByDescending(e =&gt; e.CreatedDate)
                .Select(e =&gt; e.CreatedDate)
                .FirstOrDefault();


Or, if Quote has a navigation property EmailLogs (recommended):

return dbContext.Jobs
                .Where(j =&gt; j.Guid == jobGuid)
                .SelectMany(j =&gt; j.Quotes)
                .SelectMany(q =&gt; EmailLogs)
                .OrderByDescending(e =&gt; e.CreatedDate)
                .Select(e =&gt; e.CreatedDate)
                .FirstOrDefault();




1 Even though you cast the expression tree to IEnumerable, it's still an IQueryable containing an expression.