Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core: Must be reducible node

I am not sure why I am getting the error: "Must be reducible node"

This is my query. I am running Core 2 with EF Core 2.2 (So I should have the fixes that occurred in previous versions)

    IQueryable<Gizmo> gizmos = _context.Gizmo;


    IQueryable<GizmoViewModel> dataReferences = (
        gizmos.SelectMany(j => j.DataReferences.Select(r =>
            new GizmoViewModel()
            {
                GizmoId = j.Id,
                DataId = r.DataId
            }
        ))
    );
like image 290
PrivateJoker Avatar asked Dec 19 '18 16:12

PrivateJoker


1 Answers

Simply (and sadly) you are hitting one of the current EF Core query translation bugs.

Looks like it's caused by the accessing the outer SelectMany lambda parameter inside the inner Select expression.

The workaround is to use another SelectMany overload having a second lambda with both outer and inner parameters (which I guess is used by C# compiler when converting LINQ query syntax):

IQueryable<GizmoViewModel> dataReferences = (
    gizmos.SelectMany(j => j.DataReferences, (j, r) =>
        new GizmoViewModel()
        {
            GizmoId = j.Id,
            DataId = r.DataId
        }
    )
);
like image 186
Ivan Stoev Avatar answered Sep 20 '22 18:09

Ivan Stoev