zaria khan zaria khan - 24 days ago 7
C# Question

Assign a List object to another List in c#

I have following classes:

public class ProviderQualification
{
public List<ProviderDetail> ProviderDetails { get; set; }
}
public class ProviderDetail
{
public string ProviderName { get; set; }
public string ServiceableOffers { get; set; }
}
public class ProviderQualificationTimeViewModel
{
public List<ProviderQualificationDetail> ProviderQualificationDetails { get; set; }
}
public class ProviderQualificationDetail
{
public string ProviderName { get; set; }
public string TotalServiceableOffers { get; set; }
}


I have
ProviderQualification
object populated with
List<ProviderDetail>
.

ProviderQualification providerQualification = reporting.GetProviderQualification();


Now I want to copy this list to my
List<ProviderQualificationDetail>
. How would I do that?

Answer

You need a mapping to get from ProviderDetail to ProviderQualificationDetail. For example, if you just want to copy over those values, you can just write it inline like this:

ProviderQualification providerQualification = reporting.GetProviderQualification();

var items = providerQualification.ProviderDetails
            .Select(detail => new ProviderQualificationDetail
                {
                    ProviderName = detail.ProviderName,
                    TotalServiceableOffers = detail.ServiceableOffers
                })
            .ToList();

var viewModel = new ProviderQualificationTimeViewModel
{
    ProviderQualificationDetails = items
};