0

I'm using LinqKit with EntityFrameWorkCore to create projections with single element projections. But since some Models are nested I'm getting a stackoverflow. I tried to add a condition to the Projection method (bool mapCollusions) to prevent this, but it seems to be ignored. Does anyone have an idea how to prevent these circluar references?

public static Expression<Func<Transport, TransportModel>> GetMainProjection()
        {
            return transport => new TransportModel()
            {
                Inmate = transport.Inmate != null ? InmateProjectionsGetProjection(true).Invoke(transport.Inmate) : null,
            };

 public static Expression<Func<Inmate, InmateModel>> InmateProjectionsGetProjection(bool mapCollusions)
        {
            return inmate => new InmateModel()
            {
                Collusions = mapCollusions ? inmate.Collusions.AsQueryable()
                    .Select(collusion => CollusionProjectionsGetProjection(false).Invoke(collusion))
                    .ToList() : null     
            };
        }

public static Expression<Func<Collusion, CollusionModel>> CollusionProjectionsGetProjection(bool mapInmate)
        {
            return collusion => new CollusionModel()
            {
                Inmate = mapInmate ? InmateProjectionsGetProjection(false).Invoke(collusion.Inmate) : null,
            };
        }
Snaketec
  • 471
  • 2
  • 14

1 Answers1

1

Try the following realisation:

public static Expression<Func<Transport, TransportModel>> GetMainProjection()
{
    return transport => new TransportModel()
    {
        Inmate = transport.Inmate != null ? InmateProjectionsGetProjection(true).Invoke(transport.Inmate) : null,
    };
}

public static Expression<Func<Inmate, InmateModel>> InmateProjectionsGetProjection(bool mapCollusions)
{
    if (!mapCollusions)
        return inmate => new InmateModel();

    return inmate => new InmateModel()
    {
        Collusions = nmate.Collusions.AsQueryable()
            .Select(collusion => CollusionProjectionsGetProjection(false).Invoke(collusion))
            .ToList()
    };
}

public static Expression<Func<Collusion, CollusionModel>> CollusionProjectionsGetProjection(bool mapInmate)
{
    if (!mapInmate)
        return collusion => new CollusionModel();

    return collusion => new CollusionModel()
    {
        Inmate = InmateProjectionsGetProjection(false).Invoke(collusion.Inmate),
    };
}
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
  • That's similar to what I ended up with. I just made entirely different methods instead of using a flag. Downside to both solutions is I have to implement each mapping multiple times for the rest of the properties. And that's what I actually want to prevent when using Linqkit. – Snaketec Aug 17 '22 at 05:41
  • As I know, with current realization, you can't. – Svyatoslav Danyliv Aug 17 '22 at 09:06