I have a class that contains a list. I want to copy that list to another object that contains the same type and amount of attributes.
List<CinemaUnitSchema> cinemaUnitSchemas = new List<CinemaUnitSchema>();
foreach (CinemaUnit cinemaUnits in scenario.CinemaUnits)
{
cinemaUnitSchemas.Add(new CinemaUnitSchema
{
Name = cinemaUnits.Name,
AttendantPoints = cinemaUnits.AttendantPoints,
ShowPoints = cinemaUnits.ShowPoints
});
}
scenarioSchema.CinemaUnits.AddRange(CinemaUnitSchemas);
However, I'm receiving an error in this line of code;
AttendantPoints = cinemaUnits.AttendantPoints
The error I'm receiving is:
"Cannot implicitly convert type 'System.Collections.Generic.List < MyApp.Models.AttendantPoint >' to 'System.Collections.Generic.List < MyApp.Schemas.AttendantPointSchema >'."
Class of CinemaUnit is:
public class CinemaUnit
{
public string Name { get; set; }
public List<AttendantPoint> AttendantPoints { get; set; }
public bool ShowPoints { get; set; }
}
The Class of CinemaUnitSchema is:
public class CinemaUnitSchema
{
public string Name { get; set; }
public List<AttendantPoint> AttendantPoints { get; set; }
public bool ShowPoints { get; set; }
}
Solution Intended
Add in each iteration the respective list to the new object.
Thanks,
scenarioSchema.CinemaUnits.AddRange(CinemaUnitSchemas);- you trying add list of one type to list of different type