0

I have This object

   public class ParentGroup
    {
        public int Id { get; set; }
        public ParentGroup Parent { get; set; }

        public int? ParentId { get; set; }

        public ICollection<ParentGroup> Children { get; set; }

        public ParentGroupType ParentGroupType { get; set; }
        public int? ParentGroupTypeId { get; set; }

        public bool Active { get; set; }

        public string Code { get; set; }

        public string Name { get; set; }

    }
  • I'm trying to get all the parent with all the children but there is a something that happened with me and I can't understand if I put the where filter in the query I can't get all the data related to the children I got only the first level

  • If I removed it I get all the date with all the levels and I don't want to make the query to the DB, not my memory

This my code

  public async Task<List<ParentGroup>> GetOrganizationStructureTree()
        {

           // var query = _context.ParentGroups.Where(x => x.ParentId == null);

            List<ParentGroup> ParentGroups = await _context.ParentGroups.Include(par => par.Children).Include(par => par.Parent).Include(pr => pr.ParentGroupType).ToListAsync();


            return ParentGroups;
        }
  • what do you mean in this sentence " I don't want to make the query to the DB, not my memory"? – Besher Tobeh Nov 11 '19 at 12:13
  • check this post https://stackoverflow.com/questions/1308158/how-does-entity-framework-work-with-recursive-hierarchies-include-seems-not-t – Besher Tobeh Nov 11 '19 at 12:26
  • @BesherTobeh if I used the where filter within the query I didn't get al data related to all the levels I only get the first two levels and the remaining I got them with null so to achieve that I had to get all the data first in my memory by using toList and then filter them so the filtration completed withing my memory and I didn't want this behavior i want to make all in one transaction – Mohamed Hamada Nov 12 '19 at 06:04
  • i don't think you can do this with one hit to database ,do you check the post i have linked – Besher Tobeh Nov 12 '19 at 07:46

0 Answers0