简体   繁体   English

如何在 .NET Core 3.0 实体框架中执行组加入?

[英]How to perform a group join in .NET Core 3.0 Entity Framework?

With the changes to .NET Core 3.0 I am getting随着对 .NET Core 3.0 的更改,我得到了

... NavigationExpandingExpressionVisitor' failed. ... NavigationExpandingExpressionVisitor' 失败。 This may indicate either a bug or a limitation in EF Core.这可能表示 EF Core 中存在错误或限制。 See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.) ---> System.InvalidOperationException: Processing of the LINQ expression 'GroupJoin, ...有关更多详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101433 。)---> System.InvalidOperationException:处理 LINQ 表达式 'GroupJoin,...

This is a really simple query so there must be a way to perform it in .NET CORE 3.0:这是一个非常简单的查询,因此必须有一种方法可以在 .NET CORE 3.0 中执行它:

 var queryResults1 = await patients
            .GroupJoin(
                _context.Studies,
                p => p.Id,
                s => s.Patient.Id,
                (p, studies) => new 
                {
                    p.DateOfBirth,
                    p.Id,
                    p.Name,
                    p.Sex,
                   Studies =studies.Select(s1=>s1)
                }
            )
            .AsNoTracking().ToListAsync();

I am basically looking for a Linq query (or method syntax as above) which will join Studies onto Patients, and set Studies to an empty list or null if there are no studies for the given patient.我基本上是在寻找一个 Linq 查询(或上面的方法语法),它将 Studies 加入 Patients,并将 Studies 设置为空列表或 null 如果没有针对给定患者的研究。

Any ideas?有任何想法吗? This was working in .NET Core 2.2.这适用于 .NET Core 2.2。 Also the MSFT link above mentions that the key breaking change is related to client side evaluation and avoiding that the generated query reads entire tables which must then be joined or filtered client side.上面的 MSFT 链接还提到关键的重大变化与客户端评估有关,并避免生成的查询读取整个表,然后必须连接或过滤客户端。 However with this simple query, the join should be easily doable server side.然而,通过这个简单的查询,连接应该很容易在服务器端实现。

As discussed here , you're attempting a query that isn't supported by the database.正如这里所讨论的,您正在尝试数据库不支持的查询。 EF Core 2 used client-side evaluation to make your code work, but EF Core 3 refuses, because the client-side convenience comes at the cost of hard-to-debug performance problems as the dataset increases. EF Core 2 使用客户端评估来使您的代码工作,但 EF Core 3 拒绝,因为随着数据集的增加,客户端的便利性是以难以调试的性能问题为代价的。

You can use use DefaultIfEmpty to left join the patients' studies and then group manually with ToLookup .您可以使用DefaultIfEmpty离开加入患者的研究,然后使用ToLookup手动分组。

var query =
    from p in db.Patients
    join s in db.Studies on p.Id equals s.PatientId into studies
    from s in studies.DefaultIfEmpty()
    select new { Patient = p, Study = s };

var grouping = query.ToLookup(e => e.Patient); // Grouping done client side

The above example grabs the full Patient and Study entities, but you can cherry pick columns instead.上面的示例抓取了完整的 Patient 和 Study 实体,但您可以选择选择列。 If the data you need from Patient is too big to repeat for each Study, in the joined query select only the Patient ID, querying the rest of the Patient data in a separate non-joined query.如果您需要的来自 Patient 的数据太大而无法为每个 Study 重复,则在连接查询 select 中仅输入患者 ID,在单独的非连接查询中查询患者数据的 rest。

Had exactly the same issue and a big struggle with it.有完全相同的问题并且与之斗争。 It turns out that .net Core 3.0 does not support Join or Groupjoin in method syntax (yet?).事实证明 .net Core 3.0 在方法语法中不支持 Join 或 Groupjoin(还?)。 The fun part is though, it does work in Query syntax.有趣的是,它确实适用于查询语法。

Try this, it's query syntax with a bit of method syntax.试试这个,它是带有一些方法语法的查询语法。 This translates nicely to the correct SQL query with a nice left outer join and it is processed on the database.这很好地转换为正确的 SQL 查询,带有一个很好的左外连接,并在数据库上进行处理。 I haven't got your models so you need to check the syntax yourselves....我没有你的模型,所以你需要自己检查语法....

var queryResults1 = 
    (from p in _context.patients
    from s in _context.Studies.Where(st => st.PatientId == p.Id).DefaultIfEmpty()
    select new
    {
        p.DateOfBirth,
        p.Id,
        p.Name,
        p.Sex,
        Studies = studies.Select(s1 => s1)
    }).ToListAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在大型数据库上的 .NET Core 3.0 中执行组加入? - How to perform a group join in .NET Core 3.0 on a large database? 如何使用C#.net核心实体框架执行联接 - How to perform a join in c# .net core Entity Framework using 如何在 Entity Framework Core 3.0 中播种? - How to seed in Entity Framework Core 3.0? 如何在不检索所有行的情况下使用 Entity Framework Core 3.0 实现组分页? - How to implement pagination with group by using Entity Framework Core 3.0 without retrieving all rows? 如何通过 In EF CORE 执行与组的联接操作 - How to Perform a Join Operation with a Group by In EF CORE 实体框架核心:如何通过仅选择最新创建的记录在“ IQueryable”上执行联接? - Entity Framework Core: How to perform a Join on `IQueryable` by selecting only the latest created records? ASP.NET Core 3.0 实体框架中的存储过程 - Stored Procs in ASP.NET Core 3.0 Entity Framework 实体框架:分组和加入 - Entity Framework : Group by and Join 这是如何使用Entity Framework Core和ASP.NET Core MVC 2.2+和3.0创建数据传输对象(DTO) - Is This How to Create a Data Transfer Object (DTO) with Entity Framework Core & ASP.NET Core MVC 2.2+ and 3.0 如何在Entity Framework Core中创建group子句 - How to create group clause in Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM