Question:
How to flatten a tree into a list of objects in CSharp?

This approach makes use of recursion. 


It needs a bit of tidying up and error trapping. The data objects here are record-based value objects which simplify cloning.


public record Taxonomy(int Id, string Name, int? ParentId);


The object code:

public record TaxonomyGrid

{

    public int? L1Id { get; init; }

    public string? L1 { get; init; }

    public int? L2Id { get; init; }

    public string? L2 { get; init; }

    public int? L3Id { get; init; }

    public string? L3 { get; init; }

    public int? L4Id { get; init; }

    public string? L4 { get; init; }

    public int? L5Id { get; init; }

    public string? L5 { get; init; }

}


public class TaxonomyProvider

{

    private List<Taxonomy> _taxonomies = new List<Taxonomy>();


    public TaxonomyProvider()

    {

        _taxonomies.Add(new(1, "World", 0));

        _taxonomies.Add(new(2, "Europe", 1));

        _taxonomies.Add(new(3, "Asia", 1));

        _taxonomies.Add(new(4, "Africa", 1));

        _taxonomies.Add(new(5, "France", 2));

        _taxonomies.Add(new(6, "Spain", 2));

        _taxonomies.Add(new(7, "Portugal", 2));

        _taxonomies.Add(new(8, "Cordoba", 6));

        _taxonomies.Add(new(9, "Algeciras", 6));

    }


    private List<TaxonomyGrid> _grid = new List<TaxonomyGrid>();


    public List<TaxonomyGrid> GetGrid()

    {

        _grid = new List<TaxonomyGrid>();


        GetLevel(0, 1, new());


        return _grid;

    }


    private void GetLevel(int id, int level, TaxonomyGrid gridItem)

    {

        var items = _taxonomies.Where(item => item.ParentId == id);


        foreach (var item in items)

        {

            var nextLevel = level + 1;

            var newGridItem = GetGridItem(item, level, gridItem);

            if (newGridItem is not null)

                GetLevel(item.Id, nextLevel, newGridItem);

        }



        if (gridItem != new TaxonomyGrid())

            _grid.Add(gridItem with { });

    }


    private TaxonomyGrid? GetGridItem(Taxonomy tax, int level, TaxonomyGrid gridItem)

    {

        var grid = level switch

        {

            1 => gridItem with { L1 = tax.Name, L1Id = tax.Id },

            2 => gridItem with { L2 = tax.Name, L2Id = tax.Id },

            3 => gridItem with { L3 = tax.Name, L3Id = tax.Id },

            4 => gridItem with { L4 = tax.Name, L4Id = tax.Id },

            5 => gridItem with { L5 = tax.Name, L5Id = tax.Id },

            _ => null

        };

        return grid;

    }

}



Suggested blogs:

>How to assign multiple const variables in a single declaration in TypeScript?

>How to handle more than 100 cases neatly on Typescript?

>Type inference with 'as const' IN TypeScript

>Typescript Return argument: Tuple type or passed to rest parameter warning?

>How can you read a Blob in Deno in TypeScript?

>How to do Yup validation in form with TypeScript?

>How can I merge two arrays of objects that can be undefined in TypeScript?

>Javascript Error Solved: Property 'id' does not exist on type 'T'


Ritu Singh

Ritu Singh

Submit
0 Answers