Entity Framework 核心 Parent/Child 使用多级子项清理查询

Entity Framework Core Parent/Child Clean query with multi level child

我在清理查询我的父子节点时遇到问题,而没有在列表的根目录中重复子节点。

对象

 public class Office
    {

        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public bool Inactive { get; set; }
        public int? ParentId { get; set; }
        [ForeignKey("ParentId")]
        public Office Parent { get; set; }
        [InverseProperty("Parent")]
        public virtual ICollection<Office> Children { get; set; }
    }
}

查询

public async Task<IEnumerable<Office>> GetOffices()
        {
            return await _context.Offices.Where(os => !os.Inactive)
                   .Include(o => o.Parent)
                   .Include(o => o.Children).ToListAsync();
        }

结果

 {
        "id": 5,
        "name": "AA",
        "inactive": false,
        "parentId": null,
        "parent": null,
        "children": []
    },
    {
        "id": 10,
        "name": "BAA",
        "inactive": false,
        "parentId": 2,
        "parent": {
            "id": 2,
            "name": "BA",
            "inactive": false,
            "parentId": null,
            "parent": null,
            "children": []
        },
        "children": [
            {
                "id": 1011,
                "name": "BAAA",
                "inactive": false,
                "parentId": 10,
                "children": []
            }
        ]
    },
    {
        "id": 1011,
        "name": "BAAA",
        "inactive": false,
        "parentId": 10,
        "parent": {
            "id": 10,
            "name": "BAA",
            "inactive": false,
            "parentId": 2,
            "parent": {
                "id": 2,
                "name": "BA",
                "inactive": false,
                "parentId": null,
                "parent": null,
                "children": []
            },
            "children": []
        },
        "children": []
    },
,
    {
        "id": 2,
        "name": "BA",
        "inactive": false,
        "parentId": null,
        "parent": null,
        "children": [
            {
                "id": 10,
                "name": "BAA",
                "inactive": false,
                "parentId": 2,
                "children": [
                    {
                        "id": 1011,
                        "name": "BAAA",
                        "inactive": false,
                        "parentId": 10,
                        "children": []
                    }
                ]
            }
        ]
    }

这在根级别也有子对象,所以我尝试了这个

public async Task<IEnumerable<Office>> GetOffices()
        {
            return await _context.Offices.Where(os => !os.Inactive && !os.ParentId.HasValue).Include(o => o.Parent).Include(o => o.Children).ToListAsync();
        }

这是另一个查询的结果

{
        "id": 5,
        "name": "AA",
        "inactive": false,
        "parentId": null,
        "parent": null,
        "children": []
    },
    {
        "id": 2,
        "name": "BA",
        "inactive": false,
        "parentId": null,
        "parent": null,
        "children": [
            {
                "id": 10,
                "name": "BAA",
                "inactive": false,
                "parentId": 2,
                "children": null
            }
        ]
    }

这遗漏了 id 1010 BAAA 子对象。理想情况下,我希望它以

的形式出现
{
        "id": 5,
        "name": "AA",
        "inactive": false,
        "parentId": null,
        "parent": null,
        "children": []
    },
    {
        "id": 2,
        "name": "BA",
        "inactive": false,
        "parentId": null,
        "parent": null,
        "children": [
            {
                "id": 10,
                "name": "BAA",
                "inactive": false,
                "parentId": 2,
                "children": [
                    {
                        "id": 1011,
                        "name": "BAAA",
                        "inactive": false,
                        "parentId": 10,
                        "children": []
                    }
                ]
            }
        ]
    }

无论我如何堆叠查询,我似乎都无法以这种方式显示它。有没有一种方法可以调整查询以允许此查询命令或简单的 post 查询命令仅 trim 作为子级的根级对象?

如果您想要获取有限数量的子级别(本例中为 2 个),您可以尝试:

await _context.Offices
    .Where(os => !os.Inactive && !os.ParentId.HasValue)
    .Include(o => o.Parent)
    .Include(o => o.Children)
    .ThenInclude(o => o.Children)
    .ToListAsync()