EntityFramework (.NET Core)多条件左外连接

3

我需要通过以下多个条件匹配UserUserActionUserIdActionTypeApproved,并且仍然想将查询保持为left outer join,因为有可能没有关联的操作。在常规的.NET Entity Framework中,我会执行以下操作:

var q = from u in db.User
        join ua in db.UserActions on { u.Id, "Approved" } equals { ua.UserId, ua.ActionType } into actions
        from ua in action.DefaultIfEmpty()
        where u.Active
        select new { User = u, Actions = ua}

对于 Entity Framework 的核心版本,很遗憾它不能正常工作。我该如何在 .NET Core 中使用 EF 实现类似的目标?


你使用的是哪个 EF Core 版本? - Ivan Stoev
你收到了什么错误信息? - Tim.Tang
1个回答

4

试试这个:

var q = from u in db.User
    join ua in db.UserActions on new { UserId = u.Id, ActionType = "Approved" } equals new { ua.UserId, ua.ActionType } into actions
    from ua in actions.DefaultIfEmpty()
    where u.Active
    select new { User = u, Actions = ua}

在连接语法中,匿名类型的属性名称需要匹配。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接