Home > Blockchain >  Web API C# .net: cannot convert the model in the dto in get method
Web API C# .net: cannot convert the model in the dto in get method

Time:10-05

As I said in the title, I'm trying to convert in the get method a model object to its DTO.

My method is to get users and is the next piece of code:

// GET: api/Users
[HttpGet]
public async Task<ActionResult<IEnumerable<UserDTO>>> GetUsers()
{
    var users = _context.Users.ToList();
    var userDtos = new List<UserDTO>();
    foreach (var user in users)
    {
        userDtos.Add(new UserDTO 
        { 
            IdUser = user.UserProfessionId, 
            UserName = user.UserName, 
            UserCompany = user.UserCompany, 
            UserMail = user.UserMail, 
            UserProfession = user.UserProfession, 
            UserProfessionField = user.UserProfessionField 
        });
    }
    return userDtos;
}

These are my model and DTO for user:

namespace Sims.Models
{
    public partial class User
    {
        public User()
        {
            DataUsages = new HashSet<DataUsage>();
        }

        public long IdUser { get; set; }
        public int UserProfessionId { get; set; }
        public int UserProfessionFieldId { get; set; }
        public string? UserName { get; set; }
        public string? UserMail { get; set; }
        public string? UserCompany { get; set; }
        public byte[]? UserPicture { get; set; }

        public virtual Profession UserProfession { get; set; } = null!;
        public virtual ProfessionField UserProfessionField { get; set; } = null!;
        public virtual ICollection<DataUsage> DataUsages { get; set; }
    }
}

and

namespace sims.DTO
{
    public partial class UserDTO
    {
        public long IdUser { get; set; }
        public string? UserName { get; set; }
        public string? UserMail { get; set; }
        public string? UserCompany { get; set; }

        public virtual ProfessionDTO UserProfession { get; set; } = null!;
        public virtual ProfessionFieldDTO UserProfessionField { get; set; } = null!;
    }
}

Profession and ProfessionField are also models and have their own DTO. But in the get method, the two following lines contain the same error as it "cannot implicitly convert type '....Models.Profession' to '....DTO.ProfessionDTO'".

Do you have any idea ?

In case, here is an example of the Profession Model and DTO:

namespace Sims.Models
{
    public partial class Profession
    {
        public Profession()
        {
            ProfessionFields = new HashSet<ProfessionField>();
            Users = new HashSet<User>();
        }

        public int IdProfession { get; set; }
        public string ProfessionName { get; set; } = null!;

        public virtual ICollection<ProfessionField> ProfessionFields { get; set; }
        public virtual ICollection<User> Users { get; set; }
    }
}

and

namespace sims.DTO
{
    public class ProfessionDTO
    {
        public int IdProfession { get; set; }
        public string ProfessionName { get; set; } = null!;
    }
}

Thanks for reading

CodePudding user response:

The UserProfession property is of type ProfessionDTO:

public virtual ProfessionDTO UserProfession { get; set; } = null!;

But you're trying to populate it with an object of type Profession:

UserProfession = user.UserProfession,

Just as the error states, they are different types and can't be substituted for one another. Populate the property with an instance of ProfessionDTO instead:

UserProfession = new UserProfessionDTO
{
    IdProfession = user.UserProfession.IdProfession,
    ProfessionName = user.UserProfession.ProfessionName
},

If the user.UserProfession field is null then you'd need to check for that. For example:

UserProfession = user.UserProfession == null ?
    null as UserProfessionDTO :
    new UserProfessionDTO
    {
        IdProfession = user.UserProfession?.IdProfession,
        ProfessionName = user.UserProfession?.ProfessionName
    },
  • Related