简体   繁体   English

如何在ASP.NET MVC Web应用程序的两个表之间创建查询?

[英]How to create a query between two tables in an ASP.NET MVC web application?

I'm having trouble creating a query between my tables 'utente' and 'indirizzo_residenza' that will do: 我在创建表“ utente”和“ indirizzo_residenza”之间的查询时遇到了麻烦:

SELECT * 
FROM utente, indirizzo_residenza 
WHERE utente.idutente = indirizzo_residenza.idutente;

Right now I can only query one table or the other and return it as OkObjectResult object 现在,我只能查询一个表或另一个表,并将其作为OkObjectResult对象返回

namespace WebApplication1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UtentesController : ControllerBase
    {
        private readonly resdataContext _context;

        public UtentesController(resdataContext context)
        {
            _context = context;
        }

        // GET: api/Utentes/GetUtente
        [HttpGet("[action]")]
        public async Task<IActionResult> GetUtente()
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var address = _context.IndirizzoResidenza.FromSql("Select * from indirizzo_residenza");
            var users = _context.Utente.FromSql("SELECT * FROM utente");

            /* string q = "SELECT * from utente,indirizzo_residenza where utente.idutente=indirizzo_residenza=idutente;";
            */

            if (utente == null)
            {
                return NotFound();
            }

            return Ok(users);
        }
    }
}

How can I manage to merge those two queries? 我如何设法合并这两个查询?

Thanks 谢谢

Your solution is not the recommended way of joining tables. 您的解决方案不是推荐的联接表方式。 So, I will propose you to use the INNER JOIN: 因此,我建议您使用INNER JOIN:

SELECT * 
FROM utente
INNER JOIN indirizzo_residenza 
ON utente.idutente = indirizzo_residenza.idutente;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM