Good afternoon everyone, I am creating a service to show my user's profile data stored in my database, I am using Linq to do the procedure, but it is not returning the data of the registered user and it returns an error, I created my interfaces repository and service to be able to call the function in my controller and be able to have everything more ordered, only in the controller I am calling the function that is in my repository
this is my service
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using Salesws.Context;
using Salesws.Models;
using Salesws.Service;
using Salesws.Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace Salesws.RestControllers
{
[Route("api/[controller]")]
[ApiController]
public class LoginController : ControllerBase
{
private IConfiguration _config;
//Contexto de la base de datos SVM
private readonly SVMcontext _context;
private readonly IFechaService _fechaService;
private readonly ILambSelLiquidacionService _iamb_Sel_Liquidacion;
private readonly IPokectCargaService _pokectService;
public LoginController(IConfiguration config, SVMcontext context,IFechaService fechaService,ILambSelLiquidacionService amb_Sel_LiquidacionService,IPokectCargaService pokectService)
{
_config = config;
_context = context;
_fechaService = fechaService;
_iamb_Sel_Liquidacion = amb_Sel_LiquidacionService;
//pokectservice
_pokectService = pokectService;
}
//buscar Usuario por id
//servicio buscar perfil
//[Authorize]
[HttpGet("ugv_GetSetDSyncPerfil/{id}")]
public ActionResult ugv_GetSetDSyncPerfil(string id)
{
var usuario = _pokectService.getDSCargaPerfil(id);
return Ok(usuario);
}
}
}
this is my repository interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Salesws.Service.Interfaces
{
public interface IPokectCargaRepository
{
public string getDSCargaPerfil(string id);
}
}
this is my service interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Salesws.Service.Interfaces
{
public interface IPokectCargaService
{
public string getDSCargaPerfil(string id);
}
}
this is my service
using Salesws.Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Salesws.Service
{
public class PokectCargaService : IPokectCargaService
{
private readonly IPokectCargaRepository _pokectRepository;
public PokectCargaService(IPokectCargaRepository pokectRepository)
{
_pokectRepository = pokectRepository;
}
public string getDSCargaPerfil(string id)
{
return _pokectRepository.getDSCargaPerfil(id);
}
}
}
this is my repository
using Microsoft.EntityFrameworkCore;
using Salesws.Context;
using Salesws.Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
namespace Salesws.Repository
{
public class PokectCargaRepository :IPokectCargaRepository
{
private readonly SVMcontext _context;
public PokectCargaRepository(SVMcontext context)
{
_context = context;
}
public string getDSCargaPerfil(string id)
{
var data = "{}";
try
{
var qry = from usuario in _context.AfcmaPersonas
where usuario.CodUsuario.Equals(id)
select usuario;
return qry.ToString();
}
catch (Exception e)
{
}
return data;
}
}
}
this is the error that I get in the postman, it gives me ok but it does not show me the user data
The problem is that it
qry
is the instance of the query . What you return is the oneToString
from the query , not the objectAfcmaPersonas
.In your case, if you only want to receive a single instance, you can use
FirstOrDefault
.Edit 1
As I said in the comment, if you use
ToString
and don't implement that method, it will only return the name of the class...To serialize it to JSON, you can use the package
Newtonsoft
On the other hand, you can implement the method
ToString
Don't forget to add the Newtonsoft package and add the
using
.I hope it works.