Entity framework core EF核心聚合查询

Entity framework core EF核心聚合查询,entity-framework-core,Entity Framework Core,我使用EF Core 5,这个查询抛出一个异常。Id和EntityId在两个表中都是唯一标识符 查询: var records = await (from r in _dbContext.Set<MedicalRecord>() join d in _dbContext.Set<Document>() on r.Id equals d.

我使用EF Core 5,这个查询抛出一个异常。Id和EntityId在两个表中都是唯一标识符

查询:

            var records = await (from r in _dbContext.Set<MedicalRecord>()
                          join d in _dbContext.Set<Document>()
                               on r.Id equals d.EntityId into grouping
                          select new { r, DocumentCount = grouping.Count() }).ToListAsync();
var records=await(来自_dbContext.Set()中的r)
在_dbContext.Set()中加入d
在r.Id等于d.EntityId的情况下进行分组
选择new{r,DocumentCount=grouping.Count()});
这里有一个例外:

System.InvalidOperationException: The LINQ expression 'DbSet<MedicalRecord>()
    .GroupJoin(
        inner: DbSet<Document>(), 
        outerKeySelector: r => r.Id, 
        innerKeySelector: d => d.EntityId, 
        resultSelector: (r, grouping) => new { 
            r = r, 
            DocumentCount = grouping
                .AsQueryable()
                .Count()
         })' 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'.
System.invalidoOperationException:LINQ表达式'DbSet())
.GroupJoin(
内部:DbSet(),
outerKeySelector:r=>r.Id,
innerKeySelector:d=>d.EntityId,
结果选择器:(r,分组)=>new{
r=r,
DocumentCount=分组
.AsQueryable()
.Count()
})“无法翻译。以可以翻译的形式重写查询,或者通过插入对“AsEnumerable”、“asAsAsAsyncEnumerable”、“ToList”或“ToListSync”的调用显式切换到客户端计算。

谢谢

GroupJoin
不是分组查询。你真的需要完整的医疗记录吗?@SvyatoslavDanyliv这很有趣。不,我可以稍后绘制医疗记录。如何创建一个SQL查询来连接两个表并对其进行分组?通过分组,您只能返回属于分组键的字段。因此,请确保需要哪些字段,并根据这些字段进行分组。如果你有原始SQL,我可以在这里帮助你翻译成LINQ。这是因为他们(EF核心团队)不愿意翻译
GroupJoin
。如果您有一个相关的
SelectMany
+
Count
(或者只使用
Count
和相关谓词),请使用集合导航属性。@SvyatoslavDanyliv
GroupJoin
GroupBy
不同。请不要将它们与EF核心团队放在同一个框中。您希望在这里使用什么样的SQL查询来“提供帮助”?具有正确navugation属性的LINQ查询很简单,如
\u dbContext.Set().Select(r=>new{r,Count=r.Documents.Count()}