skip to Main Content

I am trying to perform this Query and this is the result that I get from that:

SELECT dbo.Show.ShowId, dbo.Show.Name, dbo.Cast.CastId, dbo.Cast.Name, dbo.Cast.Birthday
FROM dbo.Show
INNER JOIN dbo.Cast ON dbo.Cast.ShowId = dbo.Show.ShowId
WHERE dbo.Cast.ShowId=1;

This is the output of this query
Output of the query

I am trying to write this query However this does not work

        [HttpGet("{id}")]
        public async Task<IActionResult> GetShowCast(long? showId)
        {
            var query = from s in db.Set<Show>()
                        join c in db.Set<Cast>()
                            on s.ShowId equals c.ShowId
                        where c.CastId == showId
                        select new { c.Birthday, s.Name};
            return Json(query);
        }

The database is working because this does work.

        [HttpGet]
        public async Task<IActionResult> Index()
        {
            return Json(await db.shows.ToListAsync());
        }

What is wrong with my query?

What is wrong with my query?
I expect to receive this from query

After updating my query i still don’t receive a result

       [HttpGet("{id}")]
        public async Task<IActionResult> GetShowCast(long? showId)
        {
            var query = from s in db.Set<Show>()
                        join c in db.Set<Cast>()
                            on s.ShowId equals c.ShowId
                        where s.ShowId == showId
                        select new { c.Birthday, s.Name };
            return Json(query.ToList());
        }

When i debug:
When debug this is see that var result is receiving nothing Count = 0

My models:

  [JsonProperty("id")]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long CastId { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("birthday")]
        public string? Birthday { get; set; }

        public long ShowId { get; set; }
        public ICollection<Show> Show { get; set; }

Model Show

      [JsonProperty("id")]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long ShowId { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        public ICollection<Cast> Casts { get; set; }

2

Answers


  1. Chosen as BEST ANSWER

    There where multiple typo's that I made and GetShowCast(long? showId) should be GetShowCast(long showId)

    The code bellow is working!

            [HttpGet("{id}")]
            public async Task<IActionResult> GetShowCast(long showId)
            {   
                var query = from s in db.Set<Show>()
                            join c in db.Set<Cast>()
                                on s.ShowId equals c.ShowId
                            where c.ShowId == 1
                            select new { c.Birthday, s.Name };
    
                var result = query.ToList();
                return Json(result);
            }
    

  2. Try:

    [HttpGet("{id}")]//remove it
            public async Task<IActionResult> GetShowCast(long? showId)
            {
                var query = from s in db.Set<Show>()
                            join c in db.Set<Cast>()
                                on s.ShowId equals c.ShowId
                            where s.ShowId == showId
                            select new { c.Birthday, s.Name };
                return Json(query);
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search