EF Core 3.0 Linq无法转换为子集合

时间:2019-11-27 21:51:02

标签: c# linq ef-core-3.0

我知道从EF Core 2.2到EF Core 3.0的重大变化,但是以下查询对我来说似乎并不复杂,因为它是SQL中的简单连接。

我有需要向其发送电子邮件的收件人列表。这些收件人已分组,我可以将组列表添加到电子邮件警报中。

此查询出了什么问题?

var emailAlert = new EmailAlert();

var emailRecipientMailboxes = _emailRecipientRepository
  .Where(x => x.GroupedRecipients.Any(erg => emailAlert.RecipientGroups
  .Any(aerg => aerg.RecipientGroupId == erg.RecipientGroupId)))
  .ToList() // Added so EFCore translates this on the client
  .Select(x => new MailboxAddress(x.FullName, x.Email))
  .ToList();

这是堆栈跟踪:

System.InvalidOperationException: The LINQ expression 'DbSet<GroupedRecipient>
    .Where(g => EF.Property<Nullable<Guid>>((EntityShaperExpression: 
        EntityType: Recipient
        ValueBufferExpression: 
            (ProjectionBindingExpression: EmptyProjectionMember)
        IsNullable: False
    ), "Id") != null && EF.Property<Nullable<Guid>>((EntityShaperExpression: 
        EntityType: Recipient
        ValueBufferExpression: 
            (ProjectionBindingExpression: EmptyProjectionMember)
        IsNullable: False
    ), "Id") == EF.Property<Nullable<Guid>>(g, "RecipientId"))
    .Any(g => __alert_RecipientGroups_0
        .Any(aerg => aerg.RecipientGroupId == g.RecipientGroupId))' 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(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

顺便提一下,在哪里可以找到如何将查询转换为适用于EFCore 3.0的查询?也许某种指南概述了哪些类型的查询不起作用以及如何解决这些问题的示例?

0 个答案:

没有答案