首页 > 解决方案 > Group by 语句无法翻译成 SQL

问题描述

EF Core 3.1 中是否有办法让如下所示的 group by 语句起作用?

 .GroupBy(x => x.Id, (id, item) =>
          new 
          {
              Id = id,
              LastDate = item.Max(z => z.Date)
          })

Id 是 auniqueidentifier并且 Date 是 adatetime2(7)

目前无法将其转换为 SQL 并出现错误

The LINQ expression 'DbSet<Delivery>
    .GroupBy(
        source: p => p.Id, 
        keySelector: p => p, 
        elementSelector: (x, y) => new { 
            Id = x, 
            LastDate = y
                .Max(z => z.Date)
         })' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().

标签: c#group-bymaxef-core-3.1entity-framework-core-3.1

解决方案


推荐阅读