asp.net 实体核心框架查询不正确?

hgqdbh6s  于 2023-03-20  发布在  .NET
关注(0)|答案(2)|浏览(177)

我正在尝试执行此查询,这是我从中得到的结果:

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;

这是

查询的输出
我正在尝试编写此查询,但这不起作用

[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);
        }

数据库正在工作,因为这确实有效。

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

我的查询有什么问题?
我的查询有什么问题?我希望从查询接收此信息
更新我的查询后,我仍然没有收到结果

[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());
        }

调试时:调试时,可以看到var result未接收任何Count = 0
我的模特:

[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; }

模特秀

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

jvlzgdj91#

我犯了多个拼写错误,GetShowCast(long? showId)应为GetShowCast(long showId)
下面的代码是工作!

[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);
        }
w8f9ii69

w8f9ii692#

试试看:

[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);
        }

相关问题