using System.Collections.Generic;
public string studentName {get; set;}
public int idCourse {get; set;}
public int id {get; set;}
public string courseName {get; set;}
public ICollection<Student> Students {get; set;} = new List<Student>();
public string courseNameDTO {get; set;}
public List<string> StudentsFromDTO {get; set;}
public static class CollectionHelpers
public static void AddRange<T>(this ICollection<T> destination,
foreach (T item in source)
public class CourseFactory
public static Course FromDTO(CourseDTO dto)
Course createdCourse = new () { courseName = dto.courseNameDTO };
createdCourse.Students.AddRange(dto.StudentsFromDTO.Select(name => new Student { idCourse = createdCourse.id, studentName = name }));
public static void Main()
courseNameDTO = "course name dto",
var course = CourseFactory.FromDTO(dto);
Console.WriteLine("Created {0}:", course);
Console.WriteLine(JsonSerializer.Serialize(course, new JsonSerializerOptions { WriteIndented = true }));