'I am trying to join two tables in ASP.NET

I want to join two tables in ASP.NET

this is my repository

   public IEnumerable<NightsAndVisitors> GetJoinedData()
        {
            var dataList = (from p in portalContext.visitors
                            join pm in portalContext.Night on p.CountryId equals pm.CountryId
                            select new NightsAndVisitors()
                           {
                              YearNo=p.YearNo,
                              MonthNo=p.MonthNo,
                              CountryId=p.CountryId,
                              CountryNameGe=p.CountryNameGe,
                              tourType=p.tourType,
                              gender=p.gender,
                              ageGroup=p.ageGroup,
                              Value=p.Value,
                              Nights=pm.Value
                          }).ToList();

            return dataList;
        }

this is my context

  public class TourismPortalContext : DbContext
    {
        public TourismPortalContext(DbContextOptions<TourismPortalContext> options):
            base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<VisitorDto>()
                .HasNoKey();
            modelBuilder.Entity<NightsDto>()
                .HasNoKey();
        }
        public DbSet<VisitorDto> visitors { get; set; }
        public DbSet<NightsDto> Night { get; set; }
    }
}

this is my controller

[HttpGet("Joined")]
        public List<NightsAndVisitors> GetJoinedData()
        {
            return NightRepository.GetJoinedData().ToList();


        }

I dont have any typeerror or somethin like that. I added migration. In models everythiing is OK. when I run localhost/project/Joined it just loading. I want to return json. can anyone help me?



Solution 1:[1]

If you are communicating with an Angular front-end, utilize ASP.NETs API response. To get JSON, you may use:

   [HttpGet("Joined")]
    public ActionResult GetJoinedData()
    {
        return Ok(NightRepository.GetJoinedData().ToList());


    }

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