Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

c# - Linq query to get nested category in json response

I have a table which I have column:

Id Name Description ParentId CreationDate ModificationDate

enter image description here

Currently I am getting data as below:

[
"cat1",
"   subcat1",
"       subsssubcat1",
"cat2",
"cat3"
]

 public IEnumerable<string> FormatTree(ILookup<int?, NestedCategory> lookup, int? parent, int indent)
    {
        return
            from c in lookup[parent]
            from t in new[] { parent.ToString().PadLeft(indent * 4) + c.Name }.Concat(FormatTree(lookup, c.Id, indent + 1))
                // from t in new[] { parent==0? ("Category:".PadLeft(indent * 4) + c.Name):("SubCategory".PadLeft(indent * 4) + c.Name ) }.Concat(FormatTree(lookup, c.Id, indent + 1))
            select t;
    }

    IEnumerable<string> INestedCategoryRepo.GetAllNestedCategories()
    {
        var lookup = _context.NestedCategories.ToLookup(x => x.ParentID);
        return FormatTree(lookup, 0,0);
    }

But it is not differentiating category subcategory. I want identifier so that it can easily identify.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...