I'm trying to take a list of doctor with locations. each row contains doctor information along with a location. Doctor "A" might have 3 locations so doctor "A" would have 3 rows. I would like to somehow group using linq to take this list to create a new doctor class with a List.
Here is my initial list. Each row duplicates ProviderId and Name if the provider has more than one location
var providerLocation = new List<ProviderLocation>
{
new ProviderLocation
{
ProviderId = "1",
FirstName = "Provider1",
AddressId = "1",
City = "Des Moines"
},
new ProviderLocation
{
ProviderId = "1",
FirstName = "Provider1",
AddressId = "2",
City = "Urbandale"
},
new ProviderLocation
{
ProviderId = "2",
FirstName = "Provider2",
AddressId = "3",
City = "Dallas"
},
new ProviderLocation
{
ProviderId = "2",
FirstName = "Provider2",
AddressId = "4",
City = "Fort Worth"
}
};
would like it to go into new classs that looks like:
public class Doctor
{
public string ProviderId { get; set; }
public string FirstName { get; set; }
public List<DoctorLocation> Locations { get; set; }
}
public class DoctorLocation
{
public string AddressId { get; set; }
public string City { get; set; }
}
Then I could reference my doctor list by:
var doctorList = List<Doctor>
Is there a way to make this happen using linq without having to loop through the list to manually populate the new classes?