How to get average, minimum and maximum with fewer database round-trips? : <p>In an ASP.NET Core Razor Pages application, using Entity Framework Core, I'm using the code below to populate a list with average, minimum and maximum values from a database. While this works OK with the relatively small volume of data recorded so far, I don't think it's going to scale well as the volume of data grows. Is there a more efficient way to do this in the application, or should I forget about doing it in the application code and get it from a stored procedure instead?</p>

<pre class="lang-cs prettyprint-override"><code>ServerStatsList = _context
    .PerfLog
    .Select(l =&gt; new ServerStats {
        CompanyName = l.CompanyName,
        ServerName = l.ServerName,
        Average = 0,
        Minimum = 0,
        Maximum = 0
    })
    .Distinct()
    .ToList();

foreach (ServerStats s in ServerStatsList)
{
    s.Average = _context
        .PerfLog
        .Where(l =&gt; l.CompanyName == s.CompanyName &amp;&amp; l.ServerName == s.ServerName)
        .Average(l =&gt; l.MemoryAvailableMBytes);
    s.Minimum = _context
        .PerfLog
        .Where(l =&gt; l.CompanyName == s.CompanyName &amp;&amp; l.ServerName == s.ServerName)
        .Min(l =&gt; l.MemoryAvailableMBytes);
    s.Maximum = _context
        .PerfLog
        .Where(l =&gt; l.CompanyName == s.CompanyName &amp;&amp; l.ServerName == s.ServerName)
        .Max(l =&gt; l.MemoryAvailableMBytes);
}
</code></pre>
