首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linqkit:防止循环引用和堆栈溢出

Linqkit:防止循环引用和堆栈溢出
EN

Stack Overflow用户
提问于 2022-08-16 13:04:16
回答 1查看 33关注 0票数 0

我使用LinqKit和EntityFrameWorkCore创建具有单个元素投影的投影。但是由于有些模型是嵌套的,所以我得到了一个堆栈溢出。我试图在投影方法(bool mapCollusions)中添加一个条件来防止这种情况,但它似乎被忽略了。有没有人知道如何防止这些圆圈引用?

代码语言:javascript
复制
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,
            };
        }
EN

回答 1

Stack Overflow用户

发布于 2022-08-16 14:43:30

尝试实现以下几点:

代码语言:javascript
复制
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),
    };
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73374463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档