First() could not be translated when accessing first group element (Linq GroupBy)

I am using EF Core with SQLite to store a bunch of Weather Reports:

public class WeatherReport
    public string ReportId { get; set; }
    public float Temperature { get; set; }
    public DateTime ReportDate { get; set; }

In my API controller, I return them like so:

IEnumerable<Models.TemperatureGetDTO> weatherReports = await _db.WeatherReports
    .Select(g => new Models.TemperatureGetDTO {
        ReportDate = g.ReportDate,
        Temperature = g.Temperature

return Ok(weatherReports);

which returns the following JSON’ified data:


Now, however, I’d like to group the temperatures by hour, and get the first weather report for every hour (grouping). I have tried writing the query like so:

var weatherReports = await _db.WeatherReports
    .GroupBy(w => w.ReportDate.Hour)
    .Select(g => new {
        Hour = g.Key,
        Temperature = g.OrderBy(w => w.ReportDate).First().Temperature

This, however, produces the following error:

The LINQ expression 'GroupByShaperExpression:
KeySelector: CAST(strftime('%H', w.ReportDate)) AS INTEGER), 
    EntityType: WeatherReport
        ProjectionBindingExpression: EmptyProjectionMember
    IsNullable: False

    .Select(s => s.Temperature)
    .First()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See for more information.

What am I doing wrong here? To get the first item from a group, should I not use First()?


It means EF cannot translate the expression into SQL so that it can be executed server side.. In the old days EF would just go and download all the data to the client and do the data manipulation there, but nowadays expressions that cannot be translated throw an exception instead.

Either force the data to be downloaded before the non-translatable expression, with something like:

(await _db.WeatherReports.GroupBy(w => w.ReportDate.Hour).ToListAsnc())
  .Select(g => new {
    Hour = g.Key,
    Temperature = g.OrderBy(w => w.ReportDate).First().Temperature


(await _db.WeatherReports.ToListAsync())
  .GroupBy(w => w.ReportDate.Hour)
  .Select(g => new {
    Hour = g.Key,
    Temperature = g.OrderBy(w => w.ReportDate).First().Temperature

Or perhaps drop to raw SQL for this, as it’s a reasonably advanced use:

  SELECT *, ROW_NUMBER() OVER(PARTITION BY strftime('%Y%m%d%H', ReportDate) ORDER BY ReportDate) rn
  FROM WeatherReports
) WHERE rn = 1

By the way, be careful grouping by only the hour as you have done in your LINQ if the data in the table spans more than a day; data from 1300hours yesterday will pollute 1300 today..

Leave a Reply

Your email address will not be published. Required fields are marked *