Parents as root elements and children as nodes in json output requirement

Viewed 21

Basically, I have a player app that now needs to link multiple children to multiple parents. But I don't want the parents to have nested children. I feel that complicates showing in a table for example.

I have for example a table

Players

Id Name
1 Adam Goldberg
2 Barry GoldBerg

Parents

Id Name
1 Berverly Gold Berg
2 Murry Gold Berg

ParentsFriends Link Table

Id PlayerId
1 1
1 1
2 2

As you see from the link Table Beverly gold berg should see both children, but Murry should only see the second child what my best way of outputting is this to json I tried similar to blow but didn't work.

 public IEnumerable<ParentsFriend> GetAllParentsFriends(int Id)
 {
   var q = from parent in _db.ParentsFriends
       from children in _db.Players
        where parent.PlayerId == children.Id
        select new ParentFriendViewModel
        {
          Parent1
          Parent2 and so on could be relatives here as well
          Children = children;
        }
   }

I want the json to output simply this this could be up to 4 or 5 parents? Parent1 Parent 2 Children=.......

This is an example of the json I want to output.

{
 The root would show all the parents first then the children in a child node
    
   "ParentId": 1,
   "title": "Murry Gold Berg",
   "description": "The best dad around",
     
   "category": "sports",
   "thumbnail": "https://dummyjson.com/image/i/products/12/thumbnail.jpg",
    ///All parents should be outputted here first then all the children below them?
    
    "ParentId2": 2,
   "title": "Berverly Gold Berg",
   "description": "The best mom around",
     
   "category": "sports",
   "thumbnail": "https://dummyjson.com/image/i/products/12/thumbnail.jpg",
   "Children": [
   "Child1":
   {
   "Name": "Barry Gold Berg",
   "AGE":18,
     .....
   },
   "Child2":
   {
   "Name": "Adam Gold Berg",
   "AGE":15,
     .....
   }
   ]
 }
1 Answers

Basically what I had to do was the following create a view model that would hold the children and the parent. Have not figured out how to do multiple yet so just going do it this way.

View Model

public class ParentFriendViewModel
{
  public IEnumerable<Person> Parents { get; set; }  
  public ICollection<Players> Children { get; set; } = new HashSet<Players>();
}

}

public List<Players> GetChildren(int? Id)
{
    List<Players> children = new List<Players>();
    var test = _db.ParentsFriends.Where(w => w.ParentId == Id );
    foreach (var item in _db.ParentsFriends.Where(w => w.ParentId == Id))
    {
        children.AddRange(_db.Players.Where(w => w.Id == item.PlayerId && 
         w.IsDeleted == false && w.IsActive == true).ToList());
    }
    return children;
}

public IEnumerable<Person> GetParents(int? Id)
{
    var q = _db.Persons.Where(w => w.Id == Id).ToList();

    var distinctItems = q.GroupBy(x => x.Id).Select(y => y.First());

    return distinctItems;
}

This is how I use it

public ParentFriendViewModel GetAllParentsFriends(int? Id)
{
    ParentFriendViewModel parentFriendViewModel = new ParentFriendViewModel();

    parentFriendViewModel.Parents = GetParents(Id);
    parentFriendViewModel.Children = GetChildren(Id);
    return parentFriendViewModel;
}

Which results in the correct json out put i desired It should be

Json Example https://jsonblob.com/1017239886792769536

Related