简体   繁体   中英

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:

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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