'How can I return two entity objects from web api?

What I am trying to do is getting two entity and returning from a single return using Json Object so getting this exception:-

Self referencing loop detected for property 'Job' with type 'System.Data.Entity.DynamicProxies

Here is what I am doing in code

        [Route("api/Listing/GetAllList/{id:Guid}")]
        [HttpGet]
        public ResponseWrapper<GenericResponseModel> GetAllList(Guid id)
        {
            
             var heightSafety = database.HeightSafetyForms.Where(j => j.JobId == id).FirstOrDefault();
            var chimneyTower = database.ChimneyTowerForms.Where(j => j.JobId == id).FirstOrDefault();
            return ResponseService.ReturnResponse(() =>
            {
                if (heightSafety == null || chimneyTower == null)
                {
                 return new GenericResponseModel(false, "Job could not be found.", null);
                }
                else
                {
                    return new GenericResponseModel(true, string.Empty, Json(new
                    {
                        heightSafety = heightSafety,
                        chimneyTower = chimneyTower
                    }));

                }
            }, Request);
        }
    }
}


Solution 1:[1]

Created ModelHelper Class for ChimneyTower and HieghtSafety and assigned the values of the object of ChimneyTower to ChimneyTowerModelHelper object and then returned in the return statement

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shalabh Mishra