Reputation: 443
These are my classes:
public class Registration
{
public bool? IsRegistered { get; set; }
public List<RegistrationProcess> RegistrationProcess { get; set; }
}
public class RegistrationProcess
{
public bool? PaidInFull { get; set; }
public double PaymentAmount { get; set; }
public bool IdentityVerified { get; set; }
}
I have a method that is doing the object mapping like this:
public Registration Translate(Services.Registration source)
{
return new Registration
{
IsRegistered = source.IsRegistered,
RegistrationProcess = new List<RegistrationProcess>
{
new RegistrationProcess()
{
PaidInFull = source.RegistrationProcess.Select(o => o.HasPaid),
}
}
};
}
I am not sure how to set up the mapping for the RegistrationProcess. I want to map PaidInFull within RegistrationProcess to the property HasPaid. They are both bools. I am getting an error: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<bool?>' to 'bool?' I feel like I need to add something to the end of the Select statement but I am not sure what. I did FirstOrDefault() and that made the error go away but I only got one value back and that is not what I want.
Upvotes: 0
Views: 2193
Reputation: 111
The problem with your approach is that you are only creating one instance of RegistrationProcess
inside the list constructor. So by calling source.RegistrationProcess.Select(o => o.HasPaid)
and assign it to your newly created RegistrationProcess
you are creating a Collection of all bool values of your service registration process and try to assign it to a single registration process.
The Solution is to create multiple RegistrationProcess
instances. In fact, one for each element in source.RegistrationProcess
. To do this you can use the Select method on source.RegistrationProcess
directly:
source.RegistrationProcesses.Select(x => new RegistrationProcess() { PaidInFull = x.HasPaid }).ToList()
As you can see, for every element in source.RegistrationProcesses
a new RegistrationProcess
is created. Or in other words: you select the elements of source.RegistrationProcesses
as new RegistrationProcess() { PaidInFull = x.HasPaid }
if that makes more sense to you.
The .ToList()
converts the IEnumerable to a list.
Upvotes: 3