gpt4 book ai didi

linq - 无法使 GroupJoin 工作。 NavigationExpandingExpressionVisitor 异常

转载 作者:行者123 更新时间:2023-12-04 03:51:24 31 4
gpt4 key购买 nike

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

[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();
抛出的异常:

Unhandled exception. System.InvalidOperationException: Processing ofthe 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. Seehttps://go.microsoft.com/fwlink/?linkid=2101433 for more detailedinformation.

最佳答案

很高兴你问。
问题是 EF Core 团队没有(也不想)提供 GroupJoin翻译。请参阅我对此 EF Core GitHub 线程的评论 Query: Support GroupJoin when it is final query operator #19930并链接了我试图说服他们提供此类支持的讨论(例如,他们为支持 LINQ left outer join 模式所做的工作应该非常容易)。所以请去那里投票,否则争论是它“没有值(value)”。
话虽如此,在当前的 EF Core 中,要么添加和使用集合导航属性(最好),要么使用相关子查询而不是 GroupJoin ,例如代替

.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) // <--
})

关于linq - 无法使 GroupJoin 工作。 NavigationExpandingExpressionVisitor 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64388869/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com