C#LINQ,带有JOIN和GroupBy的脚本引发异常

我正在处理.NET CORE 5平台上的LINQ脚本以及Entity Framework CORE 5.0.8脚本。该脚本与组一起简单左连接,但出现异常,如果不应用组,则我可以看到结果。。。不知道我在拼图中遗漏了什么

exception

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'

code

var a1 =
  (from site in db.Sites
  join machine in db.Machines on site.SiteId equals machine.SiteId into sm
  from siteMachines in sm.DefaultIfEmpty()
  where site.SiteId == SiteId
  group siteMachines by site into groupedSiteMachines
  select new
      {
       listedSite = groupedSiteMachines.Key,
       SiteMachines = groupedSiteMachines.FirstOrDefault() == null? null : groupedSiteMachines
      }
  ).ToList() ;

在.NET5中,GroupBy不会转换为SQL查询,您应该在GroupBy语句之前使用AsEnumerable.ToList

首先,您应该从SQL读取数据,而不使用任何GroupBy语句,当数据在内存中接收时,使用GroupBy

Microsoft Reference

 

转 https://www.5axxw.com/questions/content/9j38s3

posted @ 2022-07-21 09:46  dreamw  阅读(279)  评论(1)    收藏  举报