Click here to Skip to main content
14,733,443 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How to return the multiple entities for web api. i am stuck here from 3 days.

public partial class Regions
    {
        public Regions()
        {
            Projects = new HashSet<Projects>();
            RegionUserRelation = new HashSet<RegionUserRelation>();
        }

        public int RegionId { get; set; }
        [Required]
        public string RegionName { get; set; }
        [Required]
        public string RegionFullname { get; set; }
        [Required]
        public int RegionTypeId { get; set; }
        public string RegionMisc { get; set; }

        public RegionType RegionType { get; set; }
        public ICollection<Projects> Projects { get; set; }
        public ICollection<RegionUserRelation> RegionUserRelation { get; set; }
    }

public partial class Projects
    {
        public Projects()
        {
            Sections = new HashSet<Sections>();
        }

        public Guid ProjectId { get; set; }
        [Required]
        public string ProjectName { get; set; }
        [Required]
        public string ProjectCode { get; set; }
        public string ProjectDesc { get; set; }
        [Required]
        public int RegionId { get; set; }
        [Required]
        public int EpsgId { get; set; }

        public Epsg Epsg { get; set; }
        public Regions Region { get; set; }
        public ICollection<Sections> Sections { get; set; }
    }


public partial class Sections
    {
        public Sections()
        {
            Flights = new HashSet<Flights>();
        }

        public Guid SectionId { get; set; }
        [Required]
        public string SectionName { get; set; }
        public string SectionDesc { get; set; }
        [Required]
        public Guid ProjectId { get; set; }

        public Projects Project { get; set; }
        public ICollection<Flights> Flights { get; set; }
    }



Expected output


{ "Regions" : 
  [ { 
		"region_id" : "2012",
		"region_name" : "region1",
		"region_fullname" : "21700",
		"region_type" : "MERCH",
			"Projects" : [ 
			   {     "project_id" : 2341,
					 "project_name " : "DXY",
					 "project_code " : "21700",
					 "sections" : [
								{ "section_id" : 4356,
								"section_name" : sec_name,
								"section_description" : sec_dis},
								{ "section_id" : 476,
								"section_name" : sec_name2,
								"section_description" : sec_dis2},
								{ "section_id" : 908,
								"section_name" : sec_name3,
								"section_description" : sec_dis3}
							]
				},
			"Projects" : [ 
			   {     "project_id" : 1321,
					 "project_name " : "Proj_2",
					 "project_code " : "01700",
					 "sections" : [
								{ "section_id" : 4356,
								"section_name" : sec_name,
								"section_description" : sec_dis},
								{ "section_id" : 476,
								"section_name" : sec_name2,
								"section_description" : sec_dis2},
								{ "section_id" : 908,
								"section_name" : sec_name3,
								"section_description" : sec_dis3}
							]
				},
			]
  },
  { 
		"region_id" : "2013",
		"region_name" : "region3",
		"region_fullname" : "21330",
		"region_type" : "MERCH",
			"Projects" : [ 
			   {     "project_id" : 2341,
					 "project_name " : "DXY",
					 "project_code " : "21700",
					 "sections" : [
								{ "section_id" : 4356,
								"section_name" : sec_name,
								"section_description" : sec_dis},
								{ "section_id" : 476,
								"section_name" : sec_name2,
								"section_description" : sec_dis2},
								{ "section_id" : 908,
								"section_name" : sec_name3,
								"section_description" : sec_dis3}
							]
				},
			"Projects" : [ 
			   {     "project_id" : 1321,
					 "project_name " : "Proj_2",
					 "project_code " : "01700",
					 "sections" : [
								{ "section_id" : 4356,
								"section_name" : sec_name,
								"section_description" : sec_dis},
								{ "section_id" : 476,
								"section_name" : sec_name2,
								"section_description" : sec_dis2},
								{ "section_id" : 908,
								"section_name" : sec_name3,
								"section_description" : sec_dis3}
							]
				},
			]
  },
  { 
		"region_id" : "2012",
		"region_name" : "region2",
		"region_fullname" : "24300",
		"region_type" : "MERCH",
			"Projects" : [ 
			   {     "project_id" : 2341,
					 "project_name " : "DXY",
					 "project_code " : "21700",
					 "sections" : [
								{ "section_id" : 4356,
								"section_name" : sec_name,
								"section_description" : sec_dis},
								{ "section_id" : 476,
								"section_name" : sec_name2,
								"section_description" : sec_dis2},
								{ "section_id" : 908,
								"section_name" : sec_name3,
								"section_description" : sec_dis3}
							]
				},
			"Projects" : [ 
			   {     "project_id" : 1321,
					 "project_name " : "Proj_2",
					 "project_code " : "01700",
					 "sections" : [
								{ "section_id" : 4356,
								"section_name" : sec_name,
								"section_description" : sec_dis},
								{ "section_id" : 476,
								"section_name" : sec_name2,
								"section_description" : sec_dis2},
								{ "section_id" : 908,
								"section_name" : sec_name3,
								"section_description" : sec_dis3}
							]
				},
			]
  },
] }


What I have tried:

try
            {
                using (var entities = new POCContext())
                {
                    var q = (from d in entities.Regions
                             join c in entities.Projects on d.RegionId equals c.RegionId
                             join s in entities.Sections on c.ProjectId equals s.ProjectId
                             select d);
Posted
Updated 29-Apr-20 11:46am
v3

1 solution

Try either:
var q = entities.Regions
    .Include(r => r.RegionType)
    .Include(r => r.Projects).ThenInclude(p => p.Sections);
or:
var q = entities.Regions.Select(r => new
{
    region_id = r.RegionId,
    region_name = r.RegionName,
    region_fullname = r.RegionFullname,
    region_type = r.RegionType.RegionTypeName,
    Projects = r.Projects.Select(p => new
    {
        project_id = p.ProjectId,
        project_name = p.ProjectName,
        project_code = p.ProjectCode,
        sections = p.Sections.Select(s => new
        {
            section_id = s.SectionId,
            section_name = s.SectionName
            section_description = s.SectionDesc,
        })
    })
});
Loading Related Data - EF Core | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900