I am trying to assign data to a class, but when executing the method it shows me the error:
Unable to cast object of type 'System.Collections.Generic.List to type 'System.Collections.Generic.ICollection
The procedure I am doing is the following:
I have the TicketData class, which among its fields has an ICollection of the Message class.
public partial class TicketData
{
public int IdTicket { get; set; }
public int? IdUser { get; set; }
public ICollection<Message> Message { get; set; }
}
To assign the values to the TicketData class I obtain the values of the Ticket and Message class.
public class Ticket
{
public int IdTicket { get; set; }
public int? IdUser { get; set; }
}
public class Message
{
public int IdTicket { get; set; }
public int? IdMessage { get; set; }
public string Text { get; set; }
}
I do this with the following methods:
var detail = await _unitOfWork.TicketsRepository.GetDetailsTicketRepository(Id);
var messageService = await _unitOfWork.MessageRepository.GetMessagesTicketRepository(Id);
List<VMessages> listMessages = messageService.ToList();
Inside VMessages is identical to class Message.
Then I manually assign the values with:
var TData = new TicketData()
{
IdTicket = detail.IdTicket,
IdUser = detail.IdUser,
Message = (ICollection<Message>)listMessages
};
But this is where it shows me the error described above. If I assign only the values of IdTicket and IdUser it works fine, but when I assign the listMessages to Message it displays the error.
Please can you tell me where I am failing, thank you.