0

I have a model with a field that has a member variable referencing the same class. Now I want to query the data and its child items but I only want to output certain members and filter the others.

Example model:

public class ContentModel
{
    public string Name;
    public string Url;
    public string x, y, z // filter this
    public IEnumerable<ContentModel> Children;
}

Expected output:

{
  "Name": "",
  "Url": "",
  "Child": {
    "Name": "",
    "Url": "",
    "Child": {
      "Name": "",
      "Url": ""
    }
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Patrick
  • 3
  • 4
  • 2
    Please share what you have tried and the code containing the list itself. In general projections are done using `.Select` – Gilad Green Apr 15 '19 at 09:37
  • Possible duplicate of [Linq Select Certain Properties Into Another Object?](https://stackoverflow.com/questions/923238/linq-select-certain-properties-into-another-object) – vahdet Apr 15 '19 at 09:37
  • Shouldn't Children be a list object or just be one child? Models are not hierarchical. They are in table format so you need to recursively get each level of hierarchical. I will wok on sample. – jdweng Apr 15 '19 at 09:42
  • It should be a IEnumerable Children; – Patrick Apr 15 '19 at 09:53
  • Possible duplicate of https://stackoverflow.com/questions/10169648/how-to-exclude-property-from-json-serialization – Cristina Alboni Apr 15 '19 at 09:59
  • Your model is wrong. See my answer. A database contains tables with rows. So the model is an IEnumerable table. The children are not IEnumerable. A database table is a two dimensional array and cells are not IEnumerable. – jdweng Apr 15 '19 at 11:03

1 Answers1

0

I've answered questions like this hundreds of times. Normally you have a flat database table where each row contains the parents name and child name. Then you create a tree recursively. Here is an example of the code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication108
{
    class Program
    {
        static DataBase db = new DataBase();
        static void Main(string[] args)
        {
            string parentName = string.Empty;
            Tree root = new Tree();
            GetTreeRecursively(parentName, root);
        }
        static void GetTreeRecursively(string parentName, Tree parent )
        {
            foreach (ContentModel model in db.contentModel.Where(x => x.parentName == parentName))
            {
                if (parent.children == null) parent.children = new List<Tree>();
                Tree newChild = new Tree();
                parent.children.Add(newChild);
                newChild.url = model.url;
                string name = model.name;
                newChild.name = name;
                GetTreeRecursively(name, newChild);
            }
        }
    }
    public class DataBase
    {
        public List<ContentModel> contentModel { get; set; }
    }
    public class ContentModel
    {
        public string parentName  { get;set;}
        public string url { get;set;}
        public string x  { get;set;}
        public string y { get;set;}
        public string z  { get;set;}
        public string name;
    }
    public class Tree
    {
        public string name { get; set; }
        public string url { get; set; }
        public List<Tree> children { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20