如何在 Entity Framework Core 3.1 中编写组连接查询

How to write group join query in Entity Framewok Core 3.1

当我这样写 linq 查询时,我收到错误消息

"System.InvalidOperationException: 'The LINQ expression 'DbSet() .GroupJoin( inner: DbSet(), outerKeySelector: d => d.Id, innerKeySelector: dc => dc.DeviceId, resultSelector: (d, dc) => new { deviceName = d.Name, cams = dc })' 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 https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

我应该如何编辑代码?

var query = context.Devices
                   .GroupJoin(context.DeviceControls,
                              d => d.Id,
                              dc => dc.DeviceId,
                              (d, dc) => new
                                         {
                                             deviceName = d.Name,
                                             Cams = dc,
                                         }).ToList();

带有 .net core 3.1 的 EF core 将不允许您进行分组,除非您在客户端进行。您需要调用 var query = context.Devices.ToList() 然后对查询变量进行组调用。如果可能的话,另一种选择是升级到 .net 6。我想你可以做一个服务器端分组。