无法使 GroupJoin 工作.NavigationExpandingExpressionVisitor 异常 [英] Can't make GroupJoin work. NavigationExpandingExpressionVisitor exception

查看:117
本文介绍了无法使 GroupJoin 工作.NavigationExpandingExpressionVisitor 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 GroupJoin.从我看到的示例来看,以基本形式使用它似乎非常简单,但是当我使用它时,我总是会遇到 NavigationExpandingExpressionVisitor 异常.举个例子:

This is my first time using GroupJoin. From the examples I have seen it seems pretty straightforward to use in its basic form but I always get a NavigationExpandingExpressionVisitor exception when I use it. Here, an example:

[Table("Users")]
public class WAUser
{
    public int Id { get; set; }
    
    //TODO: Unique key
    [NotNull]
    public string UserUuid { get; set; } 
    
    [DefaultValue(true)]
    public bool NotifyOnlineState { get; set; }
    [DefaultValue(true)]
    public bool NotifyOfflineState { get; set; }
}


public class WASubscription
{
    public int Id { get; set; }
    
    public string PackageIdentifier { get; set; } //Product package indentifier
    
    public DateTime? ExpiresAt { get; set; } //When the subscription or trial expires
    
    public bool Expired { get; set; }
    
    public bool IsTrial { get; set; }
    
    public int PhoneCount { get; set; } //Number of phones this subscriptions supplies
    
    public int UserId { get; set; }
    public WAUser User { get; set; }
}

var userSubscriptions = await dbContext.Users
                .GroupJoin(dbContext.Subscriptions,
                    u => u.Id,
                    s => s.UserId,
                    (u, subscriptions) => new
                    {
                        User = u,
                        Subscriptions = subscriptions
                    })
                .ToListAsync();

抛出的异常:

未处理的异常.System.InvalidOperationException:处理LINQ 表达式 'DbSet.GroupJoin(外部:DbSet,内部:u =>身份证,外键选择器:s =>s.UserId,内键选择器:(u,订阅)=>新的 {用户 = 你,订阅 = 订阅})' by 'NavigationExpandingExpressionVisitor' 失败.这可能表示 EF Core 中存在错误或限制.看https://go.microsoft.com/fwlink/?linkid=2101433更详细信息.

Unhandled exception. System.InvalidOperationException: Processing of the LINQ expression 'DbSet .GroupJoin( outer: DbSet, inner: u => u.Id, outerKeySelector: s => s.UserId, innerKeySelector: (u, subscriptions) => new { User = u, Subscriptions = subscriptions })' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.

推荐答案

很高兴您的提问.

问题是 EF Core 团队没有(也不想)提供 GroupJoin 翻译.请参阅我对此 EF Core GitHub 线程的评论 查询:当它是最终查询运算符时支持 GroupJoin #19930 和链接的讨论,我试图说服他们包含这种支持(例如,对于他们支持 LINQ left outer join 模式的工作来说,这应该非常容易).所以请去那里投票,否则争论是它没有价值".

The problem is that EF Core team doesn't (and does not want to) provide GroupJoin translation. See my comments on this EF Core GitHub thread Query: Support GroupJoin when it is final query operator #19930 and linked discussions where I was trying to convince them to include such support (which should have been extremely easy for what they do to support LINQ left outer join pattern for instance). So please go there and vote, otherwise the argument is that it "has no value".

话虽如此,在当前的 EF Core 中,要么添加和使用集合导航属性(优选),要么使用相关子查询而不是 GroupJoin,例如替换

With that being said, with current EF Core either add and use collection navigation property (preferable), or use correlated subquery instead of GroupJoin, e.g. replace

.GroupJoin(dbContext.Subscriptions,
    u => u.Id,
    s => s.UserId,
    (u, subscriptions) => new
    {
        User = u,
        Subscriptions = subscriptions
    })

.Select(u => new
{
    User = u,
    Subscriptions = dbContext.Subscriptions.Where(s => u.Id == s.UserId) // <--
})

这篇关于无法使 GroupJoin 工作.NavigationExpandingExpressionVisitor 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆