EntityFramework (.NET Core) left outer join by multiple conditions

.net-core c# entity-framework entity-framework-core sql

Question

I need to match User and UserAction by several criteria: UserId, ActionType, Approved, and still want to keep the query as left outer join, because the action could be missing. In regular .net entity framework I would do the following:

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}

For the Core version of Entity Framework, unfortunately, it doesn't work. How can I achieve the similar goal using EF for .NET Core?

1
3
8/21/2017 8:49:43 PM

Popular Answer

Try this:

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}

Property names of the anonymous types on either side of the join syntax need to match.

4
9/15/2017 3:12:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow